model.spec.ts 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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. let model:Model
  22. beforeEach(() => {
  23. model = new Model()
  24. })
  25. it('should not permit args without model', () => {
  26. const args:DataPersisterArgs = {
  27. type: QUERY_TYPE.MODEL
  28. }
  29. expect(() => model.normalize(args)).toThrowError('model must be present')
  30. })
  31. it('should not permit normalize without item', async () => {
  32. const args:DataPersisterArgs = {
  33. type: QUERY_TYPE.MODEL,
  34. model: User,
  35. id: 1
  36. }
  37. expect(() => model.normalize(args)).toThrowError('Item not found')
  38. })
  39. it('should normalize model to JSON', async () => {
  40. const store = createStore()
  41. const user = store.$repo(User).make()
  42. repositoryHelperMock.findItemFromModel = jest.fn().mockReturnValue(user)
  43. const args:DataPersisterArgs = {
  44. type: QUERY_TYPE.MODEL,
  45. model: User,
  46. id: 1
  47. }
  48. expect(model.normalize(args)).toStrictEqual({ id: 1, name: 'John Doe' })
  49. })
  50. })