From ad3519ea8cb4666eb7961fd1e0b300d56dd4bd02 Mon Sep 17 00:00:00 2001 From: Manuel <30572287+manuel-rw@users.noreply.github.com> Date: Mon, 16 Jan 2023 22:25:50 +0100 Subject: [PATCH] =?UTF-8?q?=E2=9C=85=20Add=20tests=20for=20utils?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/commands/play.comands.ts | 2 +- src/commands/playlist.command.ts | 2 +- src/health/health.controller.spec.ts | 2 +- .../indicators/jeyllfin.indicator.spec.ts | 2 +- src/updates/updates.service.spec.ts | 2 +- src/utils/remoteImages/remoteImages.spec.ts | 29 +++++++++++++++++++ src/utils/{ => remoteImages}/remoteImages.ts | 2 +- src/utils/stringUtils/stringUtils.spec.ts | 23 +++++++++++++++ src/utils/{ => stringUtils}/stringUtils.ts | 10 +++++-- src/utils/tests/defaultMockerToken.spec.ts | 17 +++++++++++ .../{tests.ts => tests/defaultMockerToken.ts} | 0 11 files changed, 82 insertions(+), 9 deletions(-) create mode 100644 src/utils/remoteImages/remoteImages.spec.ts rename src/utils/{ => remoteImages}/remoteImages.ts (93%) create mode 100644 src/utils/stringUtils/stringUtils.spec.ts rename src/utils/{ => stringUtils}/stringUtils.ts (52%) create mode 100644 src/utils/tests/defaultMockerToken.spec.ts rename src/utils/{tests.ts => tests/defaultMockerToken.ts} (100%) diff --git a/src/commands/play.comands.ts b/src/commands/play.comands.ts index 8ac1708..423444b 100644 --- a/src/commands/play.comands.ts +++ b/src/commands/play.comands.ts @@ -29,7 +29,7 @@ import { } from '../models/jellyfinAudioItems'; import { PlaybackService } from '../playback/playback.service'; 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'; @Command({ diff --git a/src/commands/playlist.command.ts b/src/commands/playlist.command.ts index 41a306d..eaea2f5 100644 --- a/src/commands/playlist.command.ts +++ b/src/commands/playlist.command.ts @@ -6,7 +6,7 @@ import { DiscordMessageService } from '../clients/discord/discord.message.servic import { GenericCustomReply } from '../models/generic-try-handler'; import { PlaybackService } from '../playback/playback.service'; import { Constants } from '../utils/constants'; -import { chooseSuitableRemoteImageFromTrack } from '../utils/remoteImages'; +import { chooseSuitableRemoteImageFromTrack } from '../utils/remoteImages/remoteImages'; import { trimStringToFixedLength } from '../utils/stringUtils'; import { formatMillisecondsAsHumanReadable } from '../utils/timeUtils'; diff --git a/src/health/health.controller.spec.ts b/src/health/health.controller.spec.ts index f6f3951..d550562 100644 --- a/src/health/health.controller.spec.ts +++ b/src/health/health.controller.spec.ts @@ -5,7 +5,7 @@ import { } from '@nestjs/terminus'; import { HealthCheckExecutor } from '@nestjs/terminus/dist/health-check/health-check-executor.service'; import { Test } from '@nestjs/testing'; -import { useDefaultMockerToken } from '../utils/tests'; +import { useDefaultMockerToken } from '../utils/tests/defaultMockerToken'; import { HealthController } from './health.controller'; import { DiscordHealthIndicator } from './indicators/discord.indicator'; import { JellyfinHealthIndicator } from './indicators/jellyfin.indicator'; diff --git a/src/health/indicators/jeyllfin.indicator.spec.ts b/src/health/indicators/jeyllfin.indicator.spec.ts index 9c1e96d..38b12c7 100644 --- a/src/health/indicators/jeyllfin.indicator.spec.ts +++ b/src/health/indicators/jeyllfin.indicator.spec.ts @@ -1,7 +1,7 @@ import { HealthIndicatorResult } from '@nestjs/terminus'; import { Test } from '@nestjs/testing'; import { JellyfinService } from '../../clients/jellyfin/jellyfin.service'; -import { useDefaultMockerToken } from '../../utils/tests'; +import { useDefaultMockerToken } from '../../utils/tests/defaultMockerToken'; import { JellyfinHealthIndicator } from './jellyfin.indicator'; describe('JellyfinHealthIndicator', () => { diff --git a/src/updates/updates.service.spec.ts b/src/updates/updates.service.spec.ts index 3553b60..bd8133d 100644 --- a/src/updates/updates.service.spec.ts +++ b/src/updates/updates.service.spec.ts @@ -3,7 +3,7 @@ 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 { useDefaultMockerToken } from '../utils/tests/defaultMockerToken'; import { UpdatesService } from './updates.service'; // mock axios: https://stackoverflow.com/questions/51275434/type-of-axios-mock-using-jest-typescript/55351900#55351900 diff --git a/src/utils/remoteImages/remoteImages.spec.ts b/src/utils/remoteImages/remoteImages.spec.ts new file mode 100644 index 0000000..050c060 --- /dev/null +++ b/src/utils/remoteImages/remoteImages.spec.ts @@ -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'); + }); +}); diff --git a/src/utils/remoteImages.ts b/src/utils/remoteImages/remoteImages.ts similarity index 93% rename from src/utils/remoteImages.ts rename to src/utils/remoteImages/remoteImages.ts index 707e2ed..bd1b7cb 100644 --- a/src/utils/remoteImages.ts +++ b/src/utils/remoteImages/remoteImages.ts @@ -3,7 +3,7 @@ import { RemoteImageInfo, RemoteImageResult, } from '@jellyfin/sdk/lib/generated-client/models'; -import { Track } from '../types/track'; +import { Track } from '../../types/track'; export const chooseSuitableRemoteImage = ( remoteImageResult: RemoteImageResult, diff --git a/src/utils/stringUtils/stringUtils.spec.ts b/src/utils/stringUtils/stringUtils.spec.ts new file mode 100644 index 0000000..d3adf79 --- /dev/null +++ b/src/utils/stringUtils/stringUtils.spec.ts @@ -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...'); + }); +}); diff --git a/src/utils/stringUtils.ts b/src/utils/stringUtils/stringUtils.ts similarity index 52% rename from src/utils/stringUtils.ts rename to src/utils/stringUtils/stringUtils.ts index 9c34684..b9637a3 100644 --- a/src/utils/stringUtils.ts +++ b/src/utils/stringUtils/stringUtils.ts @@ -3,7 +3,11 @@ export const trimStringToFixedLength = (value: string, maxLength: number) => { throw new Error('max length must be positive'); } - return value.length > maxLength - ? value.substring(0, maxLength - 3) + '...' - : value; + if (value.length <= maxLength) { + return value; + } + + const upperBound = maxLength - 3; + + return value.substring(0, upperBound) + '...'; }; diff --git a/src/utils/tests/defaultMockerToken.spec.ts b/src/utils/tests/defaultMockerToken.spec.ts new file mode 100644 index 0000000..f8a5aa1 --- /dev/null +++ b/src/utils/tests/defaultMockerToken.spec.ts @@ -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(); + }); +}); diff --git a/src/utils/tests.ts b/src/utils/tests/defaultMockerToken.ts similarity index 100% rename from src/utils/tests.ts rename to src/utils/tests/defaultMockerToken.ts