Skip to content

Commit

Permalink
Updated tests to get to 100% coverage.
Browse files Browse the repository at this point in the history
  • Loading branch information
moonstar-x committed Nov 20, 2024
1 parent e1d930f commit 3b3dcae
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 8 deletions.
23 changes: 23 additions & 0 deletions src/base/presence/PresenceManager.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,19 @@ describe('Base > Presence > PresenceManager', () => {
expect(clearIntervalSpy).toHaveBeenCalledWith(oldHandle);
expect((manager as unknown as { intervalHandle: number }).intervalHandle).not.toBe(oldHandle);
});

it('should call manager.update on an interval.', async () => {
jest.useFakeTimers();
const updateSpy = jest.spyOn(manager, 'update');
updateSpy.mockImplementation(() => Promise.resolve());

await manager.setRefreshInterval(1000);
jest.advanceTimersByTime(3000);

expect(manager.update).toHaveBeenCalled();
jest.clearAllTimers();
jest.useRealTimers();
});
});

describe('setPresence()', () => {
Expand All @@ -117,6 +130,16 @@ describe('Base > Presence > PresenceManager', () => {
expect(logger.info).toHaveBeenCalledWith('Presence changed to: something');
});

it('should log presence change even if no user exists.', () => {
const oldUser = client.user;
client.user = null;

manager.setPresence('something');
expect(logger.info).toHaveBeenCalledWith('Presence changed to: something');

client.user = oldUser;
});

it('should log error if presence change fails.', () => {
const expectedError = new Error('Oops');
(client.user!.setPresence as jest.Mock).mockImplementationOnce(() => {
Expand Down
2 changes: 1 addition & 1 deletion src/base/presence/PresenceResolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ export class PresenceResolver {
case 'uptime':
return this.getUptime();
default:
throw new Error('Invalid presence name provided.');
return Promise.resolve('');
}
}

Expand Down
8 changes: 7 additions & 1 deletion src/commands/ConfigureCommand.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,10 @@ describe('Commands > ConfigureCommand', () => {
update: jest.fn().mockImplementation(() => Promise.resolve())
};
const followUpResponseMock = {
awaitMessageComponent: jest.fn().mockResolvedValue(userResponseMock)
awaitMessageComponent: jest.fn().mockImplementation(({ filter }) => {
filter({ user: { id: 1 }, customId: 'configure-storefronts-enable' });
return userResponseMock;
})
};
const interaction = {
reply: jest.fn(),
Expand All @@ -96,6 +99,9 @@ describe('Commands > ConfigureCommand', () => {
guildId: '1267881983548063785',
options: {
getSubcommand: jest.fn().mockReturnValue('storefronts')
},
user: {
id: 1
}
} as unknown as GuildChatInputCommandInteraction;

Expand Down
7 changes: 7 additions & 0 deletions src/config/app.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,13 @@ describe('Config > App', () => {
expect(config.DISCORD_SHARDING_COUNT).toBe('auto');
});

it('should export valid DISCORD_SHARDING_COUNT if set to auto.', () => {
process.env = { ...mockedEnv, DISCORD_SHARDING_COUNT: 'auto' };
resetModule();

expect(config.DISCORD_SHARDING_COUNT).toBe('auto');
});

it('should export valid REDIS_URI.', () => {
expect(config.REDIS_URI).toBe('redis://localhost:6379');
});
Expand Down
11 changes: 11 additions & 0 deletions src/features/gameOffers/classes/OffersNotifier.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,17 @@ describe('Features > GameOffers > Classes > OffersNotifier', () => {
guild.shardId = 0;
});

it('should send the message if client is not sharded.', async () => {
const oldShard = client.shard;
client.shard = null;

await triggerRef.trigger!();

expect(channel.send).toHaveBeenCalled();

client.shard = oldShard;
});

it('should not send the message if channel is null.', async () => {
(guild.channels.fetch as jest.Mock).mockResolvedValueOnce(null);
await triggerRef.trigger!();
Expand Down
4 changes: 2 additions & 2 deletions src/i18n/translate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,14 @@ export const MESSAGE_KEY_TO_LOCALE: Partial<Record<MessageKey, Locale>> = {
};
const castLocaleStrings = localeStrings as LocaleMessageMap;

const getMessage = (locale: Locale, key: MessageKey, useDefault: boolean = true): IntlMessageFormat => {
const getMessage = (locale: Locale, key: MessageKey, useDefault: boolean): IntlMessageFormat => {
const messagesForLocale = castLocaleStrings[locale];
if (!messagesForLocale) {
throw new TranslatorError(`No messages for locale ${locale} exist.`);
}

const message = messagesForLocale[key];
const defaultMessage = castLocaleStrings[DEFAULT_LOCALE]?.[key];
const defaultMessage = castLocaleStrings[DEFAULT_LOCALE]![key]; // Note: We can safely null-assert here because DEFAULT_LOCALE is a key of localStrings.
const messageToReturn = useDefault ? message ?? defaultMessage : message;

if (!messageToReturn) {
Expand Down
4 changes: 0 additions & 4 deletions src/services/database/migration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,6 @@ export const runMigrations = async (migrationsDirectory: string): Promise<Migrat
await initializeMigrationTable();
const newMigrations = await getNewMigrations(migrations);

if (!newMigrations.length) {
return [];
}

return applyMigrations(migrationsDirectory, newMigrations);
};

Expand Down

0 comments on commit 3b3dcae

Please sign in to comment.