From 89266f565526ef65624f201e1384ccc87f2f73e8 Mon Sep 17 00:00:00 2001 From: Isaac Hunter Date: Wed, 6 Nov 2024 13:03:56 -0500 Subject: [PATCH] fix failing tests --- src/jukebox/tests/jukebox.controller.spec.ts | 21 +++++----- src/jukebox/tests/jukebox.gateway.spec.ts | 42 ++++++++++++++----- .../tests/track-queue.service.spec.ts | 3 +- .../track-queue/tests/trackQueue.spec.ts | 4 +- src/utils/testing/mocking.ts | 13 ++++++ 5 files changed, 60 insertions(+), 23 deletions(-) diff --git a/src/jukebox/tests/jukebox.controller.spec.ts b/src/jukebox/tests/jukebox.controller.spec.ts index 6d91c1f..2f7996c 100644 --- a/src/jukebox/tests/jukebox.controller.spec.ts +++ b/src/jukebox/tests/jukebox.controller.spec.ts @@ -1,26 +1,23 @@ import type { TestingModule } from '@nestjs/testing' import { Test } from '@nestjs/testing' import { getRepositoryToken } from '@nestjs/typeorm' +import { AppGateway } from 'src/app.gateway' import { NetworkModule } from 'src/network/network.module' import { SpotifyAccount } from 'src/spotify/entities/spotify-account.entity' import { SpotifyAuthService } from 'src/spotify/spotify-auth.service' -import type { MockType } from 'src/utils' +import { SpotifyService } from 'src/spotify/spotify.service' +import { mockCache, mockRepo, type MockType } from 'src/utils' import { AxiosProvider } from 'src/utils/providers/axios.provider' import type { Repository } from 'typeorm' import { Jukebox, JukeboxLinkAssignment } from '../entities/jukebox.entity' import { JukeboxController } from '../jukebox.controller' import { JukeboxService } from '../jukebox.service' +import { TrackQueueService } from '../track-queue/track-queue.service' describe('JukeboxController', () => { let controller: JukeboxController beforeEach(async () => { - const mockRepo: () => MockType> = jest.fn(() => ({})) - const mockAssignmentRepo: () => MockType> = jest.fn( - () => ({}), - ) - const mockSpotifyLinkRepo: () => MockType> = jest.fn(() => ({})) - const module: TestingModule = await Test.createTestingModule({ imports: [NetworkModule], controllers: [JukeboxController], @@ -28,17 +25,21 @@ describe('JukeboxController', () => { AxiosProvider, SpotifyAuthService, JukeboxService, + SpotifyService, + TrackQueueService, + AppGateway, + mockCache, { provide: getRepositoryToken(Jukebox), - useFactory: mockRepo, + useFactory: mockRepo, }, { provide: getRepositoryToken(JukeboxLinkAssignment), - useFactory: mockAssignmentRepo, + useFactory: mockRepo, }, { provide: getRepositoryToken(SpotifyAccount), - useFactory: mockSpotifyLinkRepo, + useFactory: mockRepo, }, ], }).compile() diff --git a/src/jukebox/tests/jukebox.gateway.spec.ts b/src/jukebox/tests/jukebox.gateway.spec.ts index 607e84d..8850eca 100644 --- a/src/jukebox/tests/jukebox.gateway.spec.ts +++ b/src/jukebox/tests/jukebox.gateway.spec.ts @@ -1,18 +1,40 @@ -import { Test, TestingModule } from '@nestjs/testing'; -import { JukeboxGateway } from '../jukebox.gateway'; +import { Test, TestingModule } from '@nestjs/testing' +import { getRepositoryToken } from '@nestjs/typeorm' +import { AppGateway } from 'src/app.gateway' +import { SpotifyService } from 'src/spotify/spotify.service' +import { mockCache, mockRepo } from 'src/utils' +import { Jukebox, JukeboxLinkAssignment } from '../entities/jukebox.entity' +import { JukeboxGateway } from '../jukebox.gateway' +import { JukeboxService } from '../jukebox.service' +import { TrackQueueService } from '../track-queue/track-queue.service' describe('JukeboxGateway', () => { - let gateway: JukeboxGateway; + let gateway: JukeboxGateway beforeEach(async () => { const module: TestingModule = await Test.createTestingModule({ - providers: [JukeboxGateway], - }).compile(); + providers: [ + JukeboxGateway, + TrackQueueService, + JukeboxService, + SpotifyService, + AppGateway, + mockCache, + { + provide: getRepositoryToken(Jukebox), + useFactory: mockRepo, + }, + { + provide: getRepositoryToken(JukeboxLinkAssignment), + useFactory: mockRepo, + }, + ], + }).compile() - gateway = module.get(JukeboxGateway); - }); + gateway = module.get(JukeboxGateway) + }) it('should be defined', () => { - expect(gateway).toBeDefined(); - }); -}); + expect(gateway).toBeDefined() + }) +}) diff --git a/src/jukebox/track-queue/tests/track-queue.service.spec.ts b/src/jukebox/track-queue/tests/track-queue.service.spec.ts index 6434ae5..7d9b736 100644 --- a/src/jukebox/track-queue/tests/track-queue.service.spec.ts +++ b/src/jukebox/track-queue/tests/track-queue.service.spec.ts @@ -1,5 +1,6 @@ import type { TestingModule } from '@nestjs/testing' import { Test } from '@nestjs/testing' +import { mockCache } from 'src/utils' import { TrackQueueService } from '../track-queue.service' describe('TrackQueueService', () => { @@ -7,7 +8,7 @@ describe('TrackQueueService', () => { beforeEach(async () => { const module: TestingModule = await Test.createTestingModule({ - providers: [TrackQueueService], + providers: [TrackQueueService, mockCache], }).compile() service = module.get(TrackQueueService) diff --git a/src/jukebox/track-queue/tests/trackQueue.spec.ts b/src/jukebox/track-queue/tests/trackQueue.spec.ts index 06b8ec2..005cf04 100644 --- a/src/jukebox/track-queue/tests/trackQueue.spec.ts +++ b/src/jukebox/track-queue/tests/trackQueue.spec.ts @@ -1,5 +1,5 @@ -import { TrackQueue } from '../track-queue.service' import type { Track } from '@spotify/web-api-ts-sdk' +import { TrackQueue } from '../track-queue.service' // describe('TrackQueue Tests', () => { let queue: TrackQueue @@ -72,7 +72,7 @@ const sometrack: Track = { } describe('Test TackQue', () => { - const queue = new TrackQueue('testid') + const queue = new TrackQueue([]) // queue.setGroupId("testid") // queue.push(sometrack) it('should push to queue', () => { diff --git a/src/utils/testing/mocking.ts b/src/utils/testing/mocking.ts index 805ce5c..5b8ce28 100644 --- a/src/utils/testing/mocking.ts +++ b/src/utils/testing/mocking.ts @@ -1,3 +1,16 @@ +import { CACHE_MANAGER } from '@nestjs/cache-manager' +import { Repository } from 'typeorm' + export type MockType = { [P in keyof T]?: jest.Mock } + +export const mockCache = { + provide: CACHE_MANAGER, + useValue: { + get: () => '', + set: () => jest.fn(), + }, +} + +export const mockRepo: () => MockType> = jest.fn(() => ({}))