| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960 |
- import Model from "~/services/serializer/normalizer/model";
- import {DataPersisterArgs} from "~/types/interfaces";
- import {QUERY_TYPE} from "~/types/enums";
- import {repositoryHelper} from "~/services/store/repository";
- import User from "~/tests/unit/fixture/models/User";
- import {createStore} from "~/tests/unit/Helpers";
- jest.mock('~/services/store/repository')
- const repositoryHelperMock = repositoryHelper as jest.Mocked<typeof repositoryHelper>
- describe('support()', () => {
- it('should support model query type', () =>{
- expect(Model.support(QUERY_TYPE.MODEL)).toBeTruthy()
- })
- it('should not support default type', () =>{
- expect(Model.support(QUERY_TYPE.DEFAULT)).toBeFalsy()
- })
- it('should not support enum type', () =>{
- expect(Model.support(QUERY_TYPE.ENUM)).toBeFalsy()
- })
- })
- describe('normalize()', () => {
- let model:Model
- beforeEach(()=>{
- model = new Model()
- })
- it('should not permit args without model', () => {
- const args:DataPersisterArgs = {
- type: QUERY_TYPE.MODEL,
- }
- expect(() => model.normalize(args)).toThrowError('model must be present')
- });
- it('should not permit normalize without item', async () => {
- const args:DataPersisterArgs = {
- type: QUERY_TYPE.MODEL,
- model: User,
- id: 1
- }
- expect(() => model.normalize(args)).toThrowError('Item not found')
- });
- it('should normalize model to JSON', async () => {
- const store = createStore()
- const user = store.$repo(User).make()
- repositoryHelperMock.findItemFromModel = jest.fn().mockReturnValue(user)
- const args:DataPersisterArgs = {
- type: QUERY_TYPE.MODEL,
- model: User,
- id: 1
- }
- expect(model.normalize(args)).toStrictEqual({ id: 1, name: 'John Doe' })
- });
- })
|