model.spec.ts 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. import Model from '~/services/serializer/normalizer/model'
  2. import { DataPersisterArgs } from '~/types/interfaces'
  3. import { QUERY_TYPE } from '~/types/enums'
  4. import { repositoryHelper } from '~/services/store/repository'
  5. import User from '~/tests/unit/fixture/models/User'
  6. import { createStore } from '~/tests/unit/Helpers'
  7. jest.mock('~/services/store/repository')
  8. const repositoryHelperMock = repositoryHelper as jest.Mocked<typeof repositoryHelper>
  9. describe('support()', () => {
  10. it('should support model query type', () => {
  11. expect(Model.support(QUERY_TYPE.MODEL)).toBeTruthy()
  12. })
  13. it('should not support default type', () => {
  14. expect(Model.support(QUERY_TYPE.DEFAULT)).toBeFalsy()
  15. })
  16. it('should not support enum type', () => {
  17. expect(Model.support(QUERY_TYPE.ENUM)).toBeFalsy()
  18. })
  19. })
  20. describe('normalize()', () => {
  21. it('should not permit args without model', () => {
  22. const args: DataPersisterArgs = {
  23. type: QUERY_TYPE.MODEL
  24. }
  25. expect(() => Model.normalize(args)).toThrowError('model must be present')
  26. })
  27. it('should not permit normalize without item', async () => {
  28. const args:DataPersisterArgs = {
  29. type: QUERY_TYPE.MODEL,
  30. model: User,
  31. id: 1
  32. }
  33. expect(() => Model.normalize(args)).toThrowError('Item not found')
  34. })
  35. it('should normalize model to JSON', async () => {
  36. const store = createStore()
  37. const user = store.$repo(User).make()
  38. repositoryHelperMock.findItemFromModel = jest.fn().mockReturnValue(user)
  39. const args:DataPersisterArgs = {
  40. type: QUERY_TYPE.MODEL,
  41. model: User,
  42. id: 1
  43. }
  44. expect(Model.normalize(args)).toStrictEqual({ id: 1, name: 'John Doe' })
  45. })
  46. })