urlBuilder.spec.ts 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. import UrlBuilder from '~/services/connection/urlBuilder'
  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(() => UrlBuilder.build({
  10. type: QUERY_TYPE.DEFAULT
  11. })).toThrow()
  12. })
  13. it('should return the URL concat with Root URL', () => {
  14. expect(UrlBuilder.build({
  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(() => UrlBuilder.build({
  23. type: QUERY_TYPE.ENUM
  24. })).toThrow()
  25. })
  26. it('should return the Enum URL concat with Root URL', () => {
  27. expect(UrlBuilder.build({
  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(() => UrlBuilder.build({
  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(UrlBuilder.build({
  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(() => UrlBuilder.build({
  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(UrlBuilder.build({
  62. type: QUERY_TYPE.MODEL,
  63. model: User,
  64. rootModel: Organization,
  65. rootId: 1
  66. })).toEqual('/api/organizations/1/users')
  67. })
  68. it('should return a concatenated url from a base and a tail', () => {
  69. expect(UrlBuilder.concat('/api/', 'test')).toEqual('/api/test')
  70. })
  71. it('should return a concatenated url from a base and a any number of tails', () => {
  72. expect(UrlBuilder.concat('/api/', 'test', 'foo', 'bar')).toEqual('/api/test/foo/bar')
  73. })
  74. it('the parts of the url shall be properly joined with forward slashes', () => {
  75. expect(UrlBuilder.concat('/api', 'test/', '/foo')).toEqual('/api/test/foo')
  76. })
  77. it('shall return the base url if no tail is passed', () => {
  78. expect(UrlBuilder.concat('/api')).toEqual('/api')
  79. })
  80. })
  81. })