import { HttpModule, HttpStatus } from '@nestjs/common'; import { getModelToken } from '@nestjs/mongoose'; import { Test, TestingModule } from '@nestjs/testing'; import { INewsletterSubscription } from './interface/newsletter-subscription.interface'; import { NewsletterSubscription } from './newsletter-subscription.schema'; import { NewsletterService } from './newsletter.service'; describe('NewsletterService', () => { let service: NewsletterService; const mockNewsletterModel = { create: jest.fn(), deleteOne: jest.fn(), countDocuments: jest.fn(), findOne: jest.fn(), exec: jest.fn(), find: jest.fn(), }; beforeEach(async () => { const module: TestingModule = await Test.createTestingModule({ imports: [HttpModule], providers: [ NewsletterService, { provide: getModelToken(NewsletterSubscription.name), useValue: mockNewsletterModel, }, ], }).compile(); service = module.get<NewsletterService>(NewsletterService); }); it('should be defined', () => { expect(service).toBeDefined(); }); describe('newsletterSubscribe', () => { it('it should not add subscription for email test2@test.com : already exist', async () => { const result = { email: 'test2@test.com' } as INewsletterSubscription; jest .spyOn(service, 'findOne') .mockImplementationOnce(async (): Promise<INewsletterSubscription | undefined> => result); try { await service.newsletterSubscribe('test2@test.com'); // Fail test if above expression doesn't throw anything. expect(true).toBe(false); } catch (e) { expect(e.message).toEqual('Email already exists'); expect(e.status).toEqual(HttpStatus.BAD_REQUEST); } }); it('it should add a subscription for email test2@test.com', async () => { const result: INewsletterSubscription = { email: 'test2@test.com' } as INewsletterSubscription; const _doc = { _id: 'a1aaaaa1a1', email: 'test2@test.com' }; jest .spyOn(service, 'findOne') .mockImplementationOnce(async (): Promise<INewsletterSubscription | undefined> => undefined) .mockImplementationOnce(async (): Promise<INewsletterSubscription | undefined> => result); mockNewsletterModel.create.mockResolvedValueOnce(_doc); const subscription = await service.newsletterSubscribe('test2@test.com'); expect(subscription).toEqual({ email: 'test2@test.com' }); }); }); describe('newsletterUnsubscribe', () => { it('it should not remove subscription for email test@test.com : does not exist', async () => { const result: INewsletterSubscription = undefined; jest .spyOn(service, 'findOne') .mockImplementationOnce(async (): Promise<INewsletterSubscription | undefined> => result); try { await service.newsletterUnsubscribe('test@test.com'); // Fail test if above expression doesn't throw anything. expect(true).toBe(false); } catch (e) { expect(e.message).toEqual('Invalid email'); expect(e.status).toEqual(HttpStatus.BAD_REQUEST); } }); it('it should remove a subscription for email test2@test.com', async () => { const _doc = { _id: 'a1aaaaa1a1', email: 'test2@test.com' }; const result = { email: 'test2@test.com', deleteOne: async () => _doc, } as INewsletterSubscription; jest .spyOn(service, 'findOne') .mockImplementationOnce(async (): Promise<INewsletterSubscription | undefined> => result); const subscription = await service.newsletterUnsubscribe('test2@test.com'); expect(subscription).toEqual(_doc); }); }); describe('countNewsletterSubscriptions', () => { it('it should count subscriptions', async () => { mockNewsletterModel.countDocuments.mockResolvedValueOnce(69); const count = await service.countNewsletterSubscriptions(); expect(count).toEqual(69); }); }); describe('findOne', () => { it('it should not find a subscription with email test@test.com', async () => { mockNewsletterModel.findOne.mockResolvedValueOnce(undefined); const findOneEmail = await service.findOne('test@test.com'); expect(findOneEmail).toEqual(undefined); }); it('it should find a subscription with email test2@test.com', async () => { const _doc = { _id: 'a1aaaaa1a1', email: 'test2@test.com' } as INewsletterSubscription; mockNewsletterModel.findOne.mockResolvedValueOnce(_doc); const findOneEmail = await service.findOne('test2@test.com'); expect(findOneEmail).toEqual(_doc); }); }); describe('findAll', () => { it('it should find all', async () => { const _docs = [{ _id: 'a1aaaaa1a1', email: 'test2@test.com' } as INewsletterSubscription]; mockNewsletterModel.find.mockResolvedValueOnce(_docs); const findOneEmail = await service.findAll(); expect(findOneEmail).toEqual(_docs); }); }); describe('searchNewsletterSubscription', () => { it('it should find 2 search result', async () => { const _docs = [ { _id: 'a1aaaaa1a1', email: 'test2@test.com' } as INewsletterSubscription, { _id: 'bbbbb', email: 'test@test.com' } as INewsletterSubscription, ]; mockNewsletterModel.find.mockResolvedValueOnce(_docs); const findOneEmail = await service.searchNewsletterSubscription('test'); expect(findOneEmail.length).toBe(2); }); }); });