Add tests for utils

This commit is contained in:
Manuel 2023-01-16 22:25:50 +01:00
parent 2ec9c03b48
commit ad3519ea8c
11 changed files with 82 additions and 9 deletions

View File

@ -29,7 +29,7 @@ import {
} from '../models/jellyfinAudioItems'; } from '../models/jellyfinAudioItems';
import { PlaybackService } from '../playback/playback.service'; import { PlaybackService } from '../playback/playback.service';
import { RemoteImageResult } from '@jellyfin/sdk/lib/generated-client/models'; import { RemoteImageResult } from '@jellyfin/sdk/lib/generated-client/models';
import { chooseSuitableRemoteImage } from '../utils/remoteImages'; import { chooseSuitableRemoteImage } from '../utils/remoteImages/remoteImages';
import { trimStringToFixedLength } from '../utils/stringUtils'; import { trimStringToFixedLength } from '../utils/stringUtils';
@Command({ @Command({

View File

@ -6,7 +6,7 @@ import { DiscordMessageService } from '../clients/discord/discord.message.servic
import { GenericCustomReply } from '../models/generic-try-handler'; import { GenericCustomReply } from '../models/generic-try-handler';
import { PlaybackService } from '../playback/playback.service'; import { PlaybackService } from '../playback/playback.service';
import { Constants } from '../utils/constants'; import { Constants } from '../utils/constants';
import { chooseSuitableRemoteImageFromTrack } from '../utils/remoteImages'; import { chooseSuitableRemoteImageFromTrack } from '../utils/remoteImages/remoteImages';
import { trimStringToFixedLength } from '../utils/stringUtils'; import { trimStringToFixedLength } from '../utils/stringUtils';
import { formatMillisecondsAsHumanReadable } from '../utils/timeUtils'; import { formatMillisecondsAsHumanReadable } from '../utils/timeUtils';

View File

@ -5,7 +5,7 @@ import {
} from '@nestjs/terminus'; } from '@nestjs/terminus';
import { HealthCheckExecutor } from '@nestjs/terminus/dist/health-check/health-check-executor.service'; import { HealthCheckExecutor } from '@nestjs/terminus/dist/health-check/health-check-executor.service';
import { Test } from '@nestjs/testing'; import { Test } from '@nestjs/testing';
import { useDefaultMockerToken } from '../utils/tests'; import { useDefaultMockerToken } from '../utils/tests/defaultMockerToken';
import { HealthController } from './health.controller'; import { HealthController } from './health.controller';
import { DiscordHealthIndicator } from './indicators/discord.indicator'; import { DiscordHealthIndicator } from './indicators/discord.indicator';
import { JellyfinHealthIndicator } from './indicators/jellyfin.indicator'; import { JellyfinHealthIndicator } from './indicators/jellyfin.indicator';

View File

@ -1,7 +1,7 @@
import { HealthIndicatorResult } from '@nestjs/terminus'; import { HealthIndicatorResult } from '@nestjs/terminus';
import { Test } from '@nestjs/testing'; import { Test } from '@nestjs/testing';
import { JellyfinService } from '../../clients/jellyfin/jellyfin.service'; import { JellyfinService } from '../../clients/jellyfin/jellyfin.service';
import { useDefaultMockerToken } from '../../utils/tests'; import { useDefaultMockerToken } from '../../utils/tests/defaultMockerToken';
import { JellyfinHealthIndicator } from './jellyfin.indicator'; import { JellyfinHealthIndicator } from './jellyfin.indicator';
describe('JellyfinHealthIndicator', () => { describe('JellyfinHealthIndicator', () => {

View File

@ -3,7 +3,7 @@ import axios from 'axios';
import { Client, GuildMember } from 'discord.js'; import { Client, GuildMember } from 'discord.js';
import { DiscordMessageService } from '../clients/discord/discord.message.service'; import { DiscordMessageService } from '../clients/discord/discord.message.service';
import { GithubRelease } from '../models/github-release'; import { GithubRelease } from '../models/github-release';
import { useDefaultMockerToken } from '../utils/tests'; import { useDefaultMockerToken } from '../utils/tests/defaultMockerToken';
import { UpdatesService } from './updates.service'; import { UpdatesService } from './updates.service';
// mock axios: https://stackoverflow.com/questions/51275434/type-of-axios-mock-using-jest-typescript/55351900#55351900 // mock axios: https://stackoverflow.com/questions/51275434/type-of-axios-mock-using-jest-typescript/55351900#55351900

View File

@ -0,0 +1,29 @@
import { ImageType } from '@jellyfin/sdk/lib/generated-client/models';
import { chooseSuitableRemoteImageFromTrack } from './remoteImages';
describe('remoteImages', () => {
it('chooseSuitableRemoteImageFromTrack', () => {
const remoteImage = chooseSuitableRemoteImageFromTrack({
name: 'Testing Music',
durationInMilliseconds: 6969,
jellyfinId: '7384783',
remoteImages: {
Images: [
{
Type: ImageType.Primary,
Url: 'nice picture.png',
},
{
Type: ImageType.Screenshot,
Url: 'not nice picture',
},
],
},
streamUrl: 'http://jellyfin/example-stream',
});
expect(remoteImage).not.toBeNull();
expect(remoteImage.Type).toBe(ImageType.Primary);
expect(remoteImage.Url).toBe('nice picture.png');
});
});

View File

@ -3,7 +3,7 @@ import {
RemoteImageInfo, RemoteImageInfo,
RemoteImageResult, RemoteImageResult,
} from '@jellyfin/sdk/lib/generated-client/models'; } from '@jellyfin/sdk/lib/generated-client/models';
import { Track } from '../types/track'; import { Track } from '../../types/track';
export const chooseSuitableRemoteImage = ( export const chooseSuitableRemoteImage = (
remoteImageResult: RemoteImageResult, remoteImageResult: RemoteImageResult,

View File

@ -0,0 +1,23 @@
import { trimStringToFixedLength } from './stringUtils';
describe('stringUtils', () => {
it('trimStringToFixedLengthShouldNotTrim', () => {
const trimmedString = trimStringToFixedLength('test', 20);
expect(trimmedString).toBe('test');
});
it('trimStringToFixedLengthShouldThrowError', () => {
const action = () => {
trimStringToFixedLength('testing value', 0);
};
expect(action).toThrow(Error);
});
it('trimStringToFixedLengthShouldTrimWhenLengthExceeded', () => {
const trimmedString = trimStringToFixedLength('hello world', 5);
expect(trimmedString).toBe('he...');
});
});

View File

@ -3,7 +3,11 @@ export const trimStringToFixedLength = (value: string, maxLength: number) => {
throw new Error('max length must be positive'); throw new Error('max length must be positive');
} }
return value.length > maxLength if (value.length <= maxLength) {
? value.substring(0, maxLength - 3) + '...' return value;
: value; }
const upperBound = maxLength - 3;
return value.substring(0, upperBound) + '...';
}; };

View File

@ -0,0 +1,17 @@
import { useDefaultMockerToken } from './defaultMockerToken';
describe('defaultMockerToken', () => {
it('useDefaultMockerTokenShouldbeNull', () => {
const mockerToken = useDefaultMockerToken('test');
expect(mockerToken).toBeNull();
});
it('useDefaultMockerTokenShouldReturnNull', () => {
const mockerToken = useDefaultMockerToken(() => ({
test: () => jest.fn(),
}));
expect(mockerToken).not.toBeNull();
});
});