constructUrl.spec.ts 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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. describe('invoke()', () => {
  7. describe('getDefaultUrl()', () => {
  8. it('should throw an error if URL is missing', () => {
  9. expect(() => ConstructUrl.invoke({
  10. type: QUERY_TYPE.DEFAULT
  11. })).toThrow()
  12. })
  13. it('should return the URL concat with Root URL', () => {
  14. expect(ConstructUrl.invoke({
  15. type: QUERY_TYPE.DEFAULT,
  16. url: 'users'
  17. })).toEqual('/api/users')
  18. })
  19. })
  20. describe('getEnumUrl()', () => {
  21. it('should throw an error if enumType is missing', () => {
  22. expect(() => ConstructUrl.invoke({
  23. type: QUERY_TYPE.ENUM
  24. })).toThrow()
  25. })
  26. it('should return the Enum URL concat with Root URL', () => {
  27. expect(ConstructUrl.invoke({
  28. type: QUERY_TYPE.ENUM,
  29. enumType: 'billing_type'
  30. })).toEqual('/api/enum/billing_type')
  31. })
  32. })
  33. describe('getModelUrl()', () => {
  34. it('should throw an error if model is missing', () => {
  35. expect(() => ConstructUrl.invoke({
  36. type: QUERY_TYPE.MODEL
  37. })).toThrow()
  38. })
  39. it('should return the Model URL concat with Root URL', () => {
  40. const repositoryHelperMock = repositoryHelper as jest.Mocked<typeof repositoryHelper>
  41. repositoryHelperMock.getEntity = jest.fn().mockReturnValue('users')
  42. expect(ConstructUrl.invoke({
  43. type: QUERY_TYPE.MODEL,
  44. model: User
  45. })).toEqual('/api/users')
  46. })
  47. it('should throw an error if rootModel is defined AND rootId is missing', () => {
  48. const repositoryHelperMock = repositoryHelper as jest.Mocked<typeof repositoryHelper>
  49. repositoryHelperMock.getEntity = jest.fn().mockReturnValue('users')
  50. expect(() => ConstructUrl.invoke({
  51. type: QUERY_TYPE.MODEL,
  52. model: User,
  53. rootModel: Organization
  54. })).toThrow()
  55. })
  56. it('should return the Root Model URL, Model Url concat with Root URL', () => {
  57. const repositoryHelperMock = repositoryHelper as jest.Mocked<typeof repositoryHelper>
  58. repositoryHelperMock.getEntity = jest.fn()
  59. .mockReturnValueOnce('users')
  60. .mockReturnValueOnce('organizations')
  61. expect(ConstructUrl.invoke({
  62. type: QUERY_TYPE.MODEL,
  63. model: User,
  64. rootModel: Organization,
  65. rootId: 1
  66. })).toEqual('/api/organizations/1/users')
  67. })
  68. })
  69. })