|
| 1 | +import { CONFIG_TOKEN } from '@app/common/config/config.module'; |
| 2 | +import { Test, TestingModule } from '@nestjs/testing'; |
| 3 | +import { Repository } from 'typeorm'; |
| 4 | +import { createMock, DeepMocked } from '@golevelup/nestjs-testing'; |
| 5 | +import { TagCategory } from '../tag-category/tag-category.entity'; |
| 6 | +import { initTagCategoryMockEntity } from '../../../../test/mocks/mockEntities'; |
| 7 | +import { AutomapperModule } from 'automapper-nestjs'; |
| 8 | +import { classes } from 'automapper-classes'; |
| 9 | +import { getRepositoryToken } from '@nestjs/typeorm'; |
| 10 | +import * as config from 'config'; |
| 11 | +import { TagCategoryDto } from './tag-category.dto'; |
| 12 | +import { ServiceValidationException } from '@app/common/exceptions/base.exception'; |
| 13 | +import { TagCategoryService } from './tag-category.service'; |
| 14 | + |
| 15 | +describe('TagCategoryService', () => { |
| 16 | + let service: TagCategoryService; |
| 17 | + let tagCategoryRepositoryMock: DeepMocked<Repository<TagCategory>>; |
| 18 | + let mockTagCategoryEntity; |
| 19 | + |
| 20 | + beforeEach(async () => { |
| 21 | + tagCategoryRepositoryMock = createMock<Repository<TagCategory>>(); |
| 22 | + mockTagCategoryEntity = initTagCategoryMockEntity(); |
| 23 | + |
| 24 | + const module: TestingModule = await Test.createTestingModule({ |
| 25 | + imports: [ |
| 26 | + AutomapperModule.forRoot({ |
| 27 | + strategyInitializer: classes(), |
| 28 | + }), |
| 29 | + ], |
| 30 | + providers: [ |
| 31 | + TagCategoryService, |
| 32 | + { |
| 33 | + provide: getRepositoryToken(TagCategory), |
| 34 | + useValue: tagCategoryRepositoryMock, |
| 35 | + }, |
| 36 | + { |
| 37 | + provide: CONFIG_TOKEN, |
| 38 | + useValue: config, |
| 39 | + }, |
| 40 | + ], |
| 41 | + }).compile(); |
| 42 | + |
| 43 | + service = module.get<TagCategoryService>(TagCategoryService); |
| 44 | + tagCategoryRepositoryMock.findOne.mockResolvedValue(mockTagCategoryEntity); |
| 45 | + tagCategoryRepositoryMock.findOneOrFail.mockResolvedValue(mockTagCategoryEntity); |
| 46 | + tagCategoryRepositoryMock.save.mockResolvedValue(mockTagCategoryEntity); |
| 47 | + tagCategoryRepositoryMock = module.get(getRepositoryToken(TagCategory)); |
| 48 | + }); |
| 49 | + |
| 50 | + it('should be defined', () => { |
| 51 | + expect(service).toBeDefined(); |
| 52 | + }); |
| 53 | + |
| 54 | + it('should get a tag category', async () => { |
| 55 | + expect(await service.getOneOrFail('fake')).toStrictEqual( |
| 56 | + mockTagCategoryEntity, |
| 57 | + ); |
| 58 | + }); |
| 59 | + |
| 60 | + it('should call save when an tag category is updated', async () => { |
| 61 | + const payload: TagCategoryDto = { |
| 62 | + uuid: mockTagCategoryEntity.uuid, |
| 63 | + name: mockTagCategoryEntity.name, |
| 64 | + }; |
| 65 | + |
| 66 | + const result = await service.update(mockTagCategoryEntity.uuid, payload); |
| 67 | + expect(result).toStrictEqual(mockTagCategoryEntity); |
| 68 | + expect(tagCategoryRepositoryMock.save).toHaveBeenCalledTimes(1); |
| 69 | + expect(tagCategoryRepositoryMock.save).toHaveBeenCalledWith( |
| 70 | + mockTagCategoryEntity, |
| 71 | + ); |
| 72 | + }); |
| 73 | + |
| 74 | + it('should fail update if tag category does not exist', async () => { |
| 75 | + const payload: TagCategoryDto = { |
| 76 | + uuid: mockTagCategoryEntity.uuid, |
| 77 | + name: mockTagCategoryEntity.name, |
| 78 | + }; |
| 79 | + |
| 80 | + tagCategoryRepositoryMock.findOneOrFail.mockRejectedValue( |
| 81 | + new ServiceValidationException( |
| 82 | + `Tag Category for with ${mockTagCategoryEntity.uuid} not found`, |
| 83 | + ), |
| 84 | + ); |
| 85 | + |
| 86 | + await expect( |
| 87 | + service.update(mockTagCategoryEntity.uuid, payload), |
| 88 | + ).rejects.toMatchObject( |
| 89 | + new ServiceValidationException( |
| 90 | + `Tag Category for with ${mockTagCategoryEntity.uuid} not found`, |
| 91 | + ), |
| 92 | + ); |
| 93 | + expect(tagCategoryRepositoryMock.save).toBeCalledTimes(0); |
| 94 | + }); |
| 95 | + |
| 96 | + it('should call save when tag successfully create', async () => { |
| 97 | + const payload: TagCategoryDto = { |
| 98 | + uuid: mockTagCategoryEntity.uuid, |
| 99 | + name: mockTagCategoryEntity.name, |
| 100 | + }; |
| 101 | + |
| 102 | + await service.create(payload); |
| 103 | + |
| 104 | + expect(tagCategoryRepositoryMock.save).toBeCalledTimes(1); |
| 105 | + }); |
| 106 | +}); |
0 commit comments