mirror of
https://github.com/informaticker/discord-jellyfin-bot.git
synced 2024-11-24 18:41:57 +01:00
✅ Add tests for update service #12
This commit is contained in:
parent
bf0e5e9edb
commit
2ec9c03b48
110
src/updates/updates.service.spec.ts
Normal file
110
src/updates/updates.service.spec.ts
Normal file
@ -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<typeof axios>;
|
||||
|
||||
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>(UpdatesService);
|
||||
discordClient = moduleRef.get<Client>('__inject_discord_client__');
|
||||
discordMessageService = moduleRef.get<DiscordMessageService>(
|
||||
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();
|
||||
});
|
||||
});
|
Loading…
Reference in New Issue
Block a user