constructUrl.spec.ts 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. import ConstructUrl from "~/services/connection/constructUrl";
  2. import {QUERY_TYPE} from "~/types/enums";
  3. import User from "~/tests/unit/fixture/models/User";
  4. import Organization from "~/tests/unit/fixture/models/Organization";
  5. import {repositoryHelper} from "~/services/store/repository";
  6. let $constructUrl:ConstructUrl
  7. beforeAll(async ()=>{
  8. $constructUrl = new ConstructUrl()
  9. })
  10. describe('invoke()', () =>{
  11. describe('getDefaultUrl()', () =>{
  12. it('should throw an error if URL is missing', ()=>{
  13. expect(() => $constructUrl.invoke({
  14. type:QUERY_TYPE.DEFAULT
  15. })).toThrow()
  16. })
  17. it('should return the URL concat with Root URL', ()=>{
  18. expect($constructUrl.invoke({
  19. type:QUERY_TYPE.DEFAULT,
  20. url:'users'
  21. })).toEqual('/api/users')
  22. })
  23. })
  24. describe('getEnumUrl()', () =>{
  25. it('should throw an error if enumType is missing', ()=>{
  26. expect(() => $constructUrl.invoke({
  27. type:QUERY_TYPE.ENUM
  28. })).toThrow()
  29. })
  30. it('should return the Enum URL concat with Root URL', ()=>{
  31. expect($constructUrl.invoke({
  32. type:QUERY_TYPE.ENUM,
  33. enumType: 'billing_type'
  34. })).toEqual('/api/enum/billing_type')
  35. })
  36. })
  37. describe('getModelUrl()', () =>{
  38. it('should throw an error if model is missing', ()=>{
  39. expect(() => $constructUrl.invoke({
  40. type:QUERY_TYPE.MODEL
  41. })).toThrow()
  42. })
  43. it('should return the Model URL concat with Root URL', ()=>{
  44. const repositoryHelperMock = repositoryHelper as jest.Mocked<typeof repositoryHelper>;
  45. repositoryHelperMock.getEntity = jest.fn().mockReturnValue('users')
  46. expect($constructUrl.invoke({
  47. type:QUERY_TYPE.MODEL,
  48. model: User
  49. })).toEqual('/api/users')
  50. })
  51. it('should throw an error if rootModel is defined AND rootId is missing', ()=>{
  52. const repositoryHelperMock = repositoryHelper as jest.Mocked<typeof repositoryHelper>;
  53. repositoryHelperMock.getEntity = jest.fn().mockReturnValue('users')
  54. expect(() => $constructUrl.invoke({
  55. type:QUERY_TYPE.MODEL,
  56. model: User,
  57. root_model: Organization
  58. })).toThrow()
  59. })
  60. it('should return the Root Model URL, Model Url concat with Root URL', ()=>{
  61. const repositoryHelperMock = repositoryHelper as jest.Mocked<typeof repositoryHelper>;
  62. repositoryHelperMock.getEntity = jest.fn()
  63. .mockReturnValueOnce('users')
  64. .mockReturnValueOnce('organizations')
  65. expect($constructUrl.invoke({
  66. type:QUERY_TYPE.MODEL,
  67. model: User,
  68. root_model: Organization,
  69. root_id:1
  70. })).toEqual('/api/organizations/1/users')
  71. })
  72. })
  73. })