From 2ec9c03b48a7c9d614a4b9acea396e1fdeeef857 Mon Sep 17 00:00:00 2001 From: Manuel <30572287+manuel-rw@users.noreply.github.com> Date: Mon, 16 Jan 2023 21:49:52 +0100 Subject: [PATCH] =?UTF-8?q?=E2=9C=85=20Add=20tests=20for=20update=20servic?= =?UTF-8?q?e=20#12?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/updates/updates.service.spec.ts | 110 ++++++++++++++++++++++++++++ 1 file changed, 110 insertions(+) create mode 100644 src/updates/updates.service.spec.ts diff --git a/src/updates/updates.service.spec.ts b/src/updates/updates.service.spec.ts new file mode 100644 index 0000000..3553b60 --- /dev/null +++ b/src/updates/updates.service.spec.ts @@ -0,0 +1,110 @@ +import { Test } from '@nestjs/testing'; +import axios from 'axios'; +import { Client, GuildMember } from 'discord.js'; +import { DiscordMessageService } from '../clients/discord/discord.message.service'; +import { GithubRelease } from '../models/github-release'; +import { useDefaultMockerToken } from '../utils/tests'; +import { UpdatesService } from './updates.service'; + +// mock axios: https://stackoverflow.com/questions/51275434/type-of-axios-mock-using-jest-typescript/55351900#55351900 +jest.mock('axios'); +const mockedAxios = axios as jest.MockedFunction; + +describe('UpdatesService', () => { + const OLD_ENV = process.env; + + let updatesService: UpdatesService; + let discordClient: Client; + let discordMessageService: DiscordMessageService; + + beforeEach(async () => { + jest.resetModules(); + process.env = { ...OLD_ENV }; + + const moduleRef = await Test.createTestingModule({ + providers: [UpdatesService], + }) + .useMocker((token) => { + if (token === DiscordMessageService) { + return { + client: jest.fn().mockReturnValue({}), + buildMessage: jest.fn(), + buildErrorMessage: jest.fn(), + } as DiscordMessageService; + } + + if (token === Client || token == '__inject_discord_client__') { + return { + guilds: { + cache: [ + { + fetchOwner: () => + ({ + send: jest.fn(), + user: { tag: 'test' }, + } as unknown as GuildMember), + }, + ], + }, + }; + } + + return useDefaultMockerToken(token); + }) + .compile(); + + updatesService = moduleRef.get(UpdatesService); + discordClient = moduleRef.get('__inject_discord_client__'); + discordMessageService = moduleRef.get( + DiscordMessageService, + ); + }); + + afterAll(() => { + process.env = OLD_ENV; + }); + + it('handleCronShouldNotNotifyWhenDisabledViaEnvironmentVariable', async () => { + process.env.UPDATER_DISABLE_NOTIFICATIONS = 'true'; + mockedAxios.mockResolvedValue({ + data: { + html_url: 'https://github.com', + name: 'testing release', + tag_name: '0.0.6', + published_at: '2023-01-09T22:11:25Z', + } as GithubRelease, + status: 200, + statusText: 'Ok', + headers: {}, + config: {}, + }); + + await updatesService.handleCron(); + + expect(mockedAxios).not.toHaveBeenCalled(); + expect(discordMessageService.buildMessage).not.toHaveBeenCalled(); + expect(discordMessageService.buildErrorMessage).not.toHaveBeenCalled(); + }); + + it('handleCronShouldNotifyWhenNewRelease', async () => { + process.env.UPDATER_DISABLE_NOTIFICATIONS = 'false'; + + mockedAxios.mockResolvedValue({ + data: { + html_url: 'https://github.com', + name: 'testing release', + tag_name: '0.0.6', + published_at: '2023-01-09T22:11:25Z', + } as GithubRelease, + status: 200, + statusText: 'Ok', + headers: {}, + config: {}, + }); + + await updatesService.handleCron(); + + expect(mockedAxios).toHaveBeenCalled(); + expect(discordMessageService.buildMessage).toHaveBeenCalled(); + }); +});