Skip to content

Commit

Permalink
fix failing tests
Browse files Browse the repository at this point in the history
  • Loading branch information
IkeHunter committed Nov 6, 2024
1 parent 7693af5 commit 89266f5
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 23 deletions.
21 changes: 11 additions & 10 deletions src/jukebox/tests/jukebox.controller.spec.ts
Original file line number Diff line number Diff line change
@@ -1,44 +1,45 @@
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<Repository<Jukebox>> = jest.fn(() => ({}))
const mockAssignmentRepo: () => MockType<Repository<JukeboxLinkAssignment>> = jest.fn(
() => ({}),
)
const mockSpotifyLinkRepo: () => MockType<Repository<SpotifyAccount>> = jest.fn(() => ({}))

const module: TestingModule = await Test.createTestingModule({
imports: [NetworkModule],
controllers: [JukeboxController],
providers: [
AxiosProvider,
SpotifyAuthService,
JukeboxService,
SpotifyService,
TrackQueueService,
AppGateway,
mockCache,
{
provide: getRepositoryToken(Jukebox),
useFactory: mockRepo,
useFactory: mockRepo<Jukebox>,
},
{
provide: getRepositoryToken(JukeboxLinkAssignment),
useFactory: mockAssignmentRepo,
useFactory: mockRepo<JukeboxLinkAssignment>,
},
{
provide: getRepositoryToken(SpotifyAccount),
useFactory: mockSpotifyLinkRepo,
useFactory: mockRepo<SpotifyAccount>,
},
],
}).compile()
Expand Down
42 changes: 32 additions & 10 deletions src/jukebox/tests/jukebox.gateway.spec.ts
Original file line number Diff line number Diff line change
@@ -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<Jukebox>,
},
{
provide: getRepositoryToken(JukeboxLinkAssignment),
useFactory: mockRepo<JukeboxLinkAssignment>,
},
],
}).compile()

gateway = module.get<JukeboxGateway>(JukeboxGateway);
});
gateway = module.get<JukeboxGateway>(JukeboxGateway)
})

it('should be defined', () => {
expect(gateway).toBeDefined();
});
});
expect(gateway).toBeDefined()
})
})
3 changes: 2 additions & 1 deletion src/jukebox/track-queue/tests/track-queue.service.spec.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import type { TestingModule } from '@nestjs/testing'
import { Test } from '@nestjs/testing'
import { mockCache } from 'src/utils'
import { TrackQueueService } from '../track-queue.service'

describe('TrackQueueService', () => {
let service: TrackQueueService

beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
providers: [TrackQueueService],
providers: [TrackQueueService, mockCache],
}).compile()

service = module.get<TrackQueueService>(TrackQueueService)
Expand Down
4 changes: 2 additions & 2 deletions src/jukebox/track-queue/tests/trackQueue.spec.ts
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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', () => {
Expand Down
13 changes: 13 additions & 0 deletions src/utils/testing/mocking.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,16 @@
import { CACHE_MANAGER } from '@nestjs/cache-manager'
import { Repository } from 'typeorm'

export type MockType<T> = {
[P in keyof T]?: jest.Mock<object>
}

export const mockCache = {
provide: CACHE_MANAGER,
useValue: {
get: () => '',
set: () => jest.fn(),
},
}

export const mockRepo: <T>() => MockType<Repository<T>> = jest.fn(() => ({}))

0 comments on commit 89266f5

Please sign in to comment.