model.spec.ts 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. import Model from '~/services/serializer/normalizer/model'
  2. import {AnyJson, 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. import {queryHelper} from "~/services/store/query";
  8. jest.mock('~/services/store/repository')
  9. const repositoryHelperMock = repositoryHelper as jest.Mocked<typeof repositoryHelper>
  10. jest.mock('~/services/store/query')
  11. const queryHelperMock = queryHelper as jest.Mocked<typeof queryHelper>
  12. describe('support()', () => {
  13. it('should support model query type', () => {
  14. expect(Model.support(QUERY_TYPE.MODEL)).toBeTruthy()
  15. })
  16. it('should not support default type', () => {
  17. expect(Model.support(QUERY_TYPE.DEFAULT)).toBeFalsy()
  18. })
  19. it('should not support enum type', () => {
  20. expect(Model.support(QUERY_TYPE.ENUM)).toBeFalsy()
  21. })
  22. })
  23. describe('normalize()', () => {
  24. it('should not permit args without model', () => {
  25. const args: DataPersisterArgs = {
  26. type: QUERY_TYPE.MODEL
  27. }
  28. expect(() => Model.normalize(args)).toThrowError('*args* has no model attribute')
  29. })
  30. it('should not permit normalize without item', async () => {
  31. const args:DataPersisterArgs = {
  32. type: QUERY_TYPE.MODEL,
  33. model: User,
  34. id: 1
  35. }
  36. expect(() => Model.normalize(args)).toThrowError('Item not found')
  37. })
  38. it('should find an item thanks to a repository, and normalize it to JSON', async () => {
  39. const store = createStore()
  40. const user = store.$repo(User).make()
  41. repositoryHelperMock.findItemFromModel = jest.fn().mockReturnValue(user)
  42. const args:DataPersisterArgs = {
  43. type: QUERY_TYPE.MODEL,
  44. model: User,
  45. id: 1
  46. }
  47. expect(Model.normalize(args)).toStrictEqual({ id: 1, name: 'John Doe' })
  48. })
  49. it('should find an item thanks to a query, and normalize it to JSON', async () => {
  50. const store = createStore()
  51. const user = store.$repo(User).make()
  52. const query = store.$repo(User).query()
  53. queryHelperMock.getItem = jest.fn().mockReturnValue(user)
  54. const args:DataPersisterArgs = {
  55. type: QUERY_TYPE.MODEL,
  56. model: User,
  57. id: 1,
  58. query: query
  59. }
  60. expect(Model.normalize(args)).toStrictEqual({ id: 1, name: 'John Doe' })
  61. })
  62. })
  63. describe('isPostQuery()', () => {
  64. it('should return true if args has got a temp ID', () => {
  65. const args:DataPersisterArgs = {
  66. type: QUERY_TYPE.MODEL,
  67. idTemp: 1
  68. }
  69. expect(Model.isPostQuery(args)).toBeTruthy()
  70. })
  71. it('should return false if args has not a temp ID', () => {
  72. const args:DataPersisterArgs = {
  73. type: QUERY_TYPE.MODEL
  74. }
  75. expect(Model.isPostQuery(args)).toBeFalsy()
  76. })
  77. })
  78. describe('sanitizeBeforePost()', () => {
  79. it('should return data without id', () => {
  80. const data:AnyJson = {
  81. id: 1,
  82. foo: 'bar',
  83. embedded:{
  84. id:2,
  85. bar: 'foo'
  86. }
  87. }
  88. expect(Model.sanitizeBeforePost(data, ['embedded'])).toEqual({foo:'bar', embedded:{bar: 'foo'}})
  89. })
  90. })