Skip to content

Commit

Permalink
Merge branch 'main' into dependabot/npm_and_yarn/vitest-1.0.4
Browse files Browse the repository at this point in the history
  • Loading branch information
Mnigos authored Dec 18, 2023
2 parents 8e2ef11 + 4a62f0c commit 191d5ee
Show file tree
Hide file tree
Showing 8 changed files with 127 additions and 152 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
"@nestjs/jwt": "10.2.0",
"@nestjs/passport": "10.0.3",
"@nestjs/platform-express": "^10.2.10",
"@nestjs/swagger": "7.1.16",
"@nestjs/swagger": "7.1.17",
"@nestjs/typeorm": "10.0.1",
"@vitest/coverage-v8": "1.0.1",
"axios": "1.6.2",
Expand Down
29 changes: 11 additions & 18 deletions src/modules/player/player.controller.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { Test, TestingModule } from '@nestjs/testing'
import { firstValueFrom, of } from 'rxjs'

import { PlayerController } from './player.controller'
import { PlayerService } from './player.service'
Expand Down Expand Up @@ -35,39 +34,33 @@ describe('PlayerController', () => {
})

test('should get available devices', async () => {
vi.spyOn(playerService, 'availableDevices').mockReturnValue(of(devicesMock))
vi.spyOn(playerService, 'availableDevices').mockResolvedValue(devicesMock)

expect(
await firstValueFrom(playerController.availableDevices('awd'))
).toEqual(devicesMock)
expect(await playerController.availableDevices('awd')).toEqual(devicesMock)
})

test('should get currentPlaybackState', async () => {
vi.spyOn(playerService, 'currentPlaybackState').mockReturnValue(
of(playbackStateMock)
vi.spyOn(playerService, 'currentPlaybackState').mockResolvedValue(
playbackStateMock
)

expect(
await firstValueFrom(playerController.currentPlaybackState('awd'))
).toEqual(playbackStateMock)
expect(await playerController.currentPlaybackState('awd')).toEqual(
playbackStateMock
)
})

test('should pause player', async () => {
vi.spyOn(playerService, 'pausePlayer').mockReturnValue(
of({ success: true })
)
vi.spyOn(playerService, 'pausePlayer').mockResolvedValue({ success: true })

expect(await firstValueFrom(playerController.pausePlayer('awd'))).toEqual({
expect(await playerController.pausePlayer('awd')).toEqual({
success: true,
})
})

test('should resume player', async () => {
vi.spyOn(playerService, 'resumePlayer').mockReturnValue(
of({ success: true })
)
vi.spyOn(playerService, 'resumePlayer').mockResolvedValue({ success: true })

expect(await firstValueFrom(playerController.resumePlayer('awd'))).toEqual({
expect(await playerController.resumePlayer('awd')).toEqual({
success: true,
})
})
Expand Down
87 changes: 37 additions & 50 deletions src/modules/statistics/statistics.controller.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { Test, TestingModule } from '@nestjs/testing'
import { firstValueFrom, of } from 'rxjs'

import { StatisticsController } from './statistics.controller'
import { StatisticsService } from './statistics.service'
Expand Down Expand Up @@ -48,13 +47,13 @@ describe('StatisticsController', () => {

describe('LastTracks', () => {
test('should get last tracks', async () => {
vi.spyOn(statisticsService, 'lastTracks').mockReturnValue(
of(spotifyResponseWithCursorsMockFactory(tracksMock))
vi.spyOn(statisticsService, 'lastTracks').mockResolvedValue(
spotifyResponseWithCursorsMockFactory(tracksMock)
)

expect(
await firstValueFrom(statisticsController.lastTracks('awd', {}))
).toEqual(spotifyResponseWithCursorsMockFactory(tracksMock))
expect(await statisticsController.lastTracks('awd', {})).toEqual(
spotifyResponseWithCursorsMockFactory(tracksMock)
)
})

test('should get last tracks with limit query', async () => {
Expand All @@ -65,27 +64,25 @@ describe('StatisticsController', () => {
() => trackMock
)

vi.spyOn(statisticsService, 'lastTracks').mockReturnValue(
of(spotifyResponseWithCursorsMockFactory(formattedTracksWithLimitMock))
vi.spyOn(statisticsService, 'lastTracks').mockResolvedValue(
spotifyResponseWithCursorsMockFactory(formattedTracksWithLimitMock)
)

expect(
await firstValueFrom(statisticsController.lastTracks('awd', { limit }))
).toEqual(
expect(await statisticsController.lastTracks('awd', { limit })).toEqual(
spotifyResponseWithCursorsMockFactory(formattedTracksWithLimitMock)
)
})
})

describe('TopTracks', () => {
test('should get top tracks', async () => {
vi.spyOn(statisticsService, 'topTracks').mockReturnValue(
of(spotifyResponseWithOffsetMockFactory(tracksMock))
vi.spyOn(statisticsService, 'topTracks').mockResolvedValue(
spotifyResponseWithOffsetMockFactory(tracksMock)
)

expect(
await firstValueFrom(statisticsController.topTracks('awd', {}))
).toEqual(spotifyResponseWithOffsetMockFactory(tracksMock))
expect(await statisticsController.topTracks('awd', {})).toEqual(
spotifyResponseWithOffsetMockFactory(tracksMock)
)
})

test('should get top tracks with limit query', async () => {
Expand All @@ -96,27 +93,23 @@ describe('StatisticsController', () => {
() => trackMock
)

vi.spyOn(statisticsService, 'topTracks').mockReturnValue(
of(spotifyResponseWithOffsetMockFactory(formattedTracksWithLimitMock))
vi.spyOn(statisticsService, 'topTracks').mockResolvedValue(
spotifyResponseWithOffsetMockFactory(formattedTracksWithLimitMock)
)

expect(
await firstValueFrom(statisticsController.topTracks('awd', { limit }))
).toEqual(
expect(await statisticsController.topTracks('awd', { limit })).toEqual(
spotifyResponseWithOffsetMockFactory(formattedTracksWithLimitMock)
)
})
})

describe('TopGenres', () => {
test('should get top genres', async () => {
vi.spyOn(statisticsService, 'topGenres').mockReturnValue(
of(topGenresMock)
)
vi.spyOn(statisticsService, 'topGenres').mockResolvedValue(topGenresMock)

expect(
await firstValueFrom(statisticsController.topGenres('awd', {}))
).toEqual(topGenresMock)
expect(await statisticsController.topGenres('awd', {})).toEqual(
topGenresMock
)
})

test('should get top genres with limit argument', async () => {
Expand All @@ -126,25 +119,25 @@ describe('StatisticsController', () => {
genres: Array.from({ length: limit }, () => 'genre'),
}

vi.spyOn(statisticsService, 'topGenres').mockReturnValue(
of(genresWithLimitMock)
vi.spyOn(statisticsService, 'topGenres').mockResolvedValue(
genresWithLimitMock
)

expect(
await firstValueFrom(statisticsController.topGenres('awd', { limit }))
).toEqual(genresWithLimitMock)
expect(await statisticsController.topGenres('awd', { limit })).toEqual(
genresWithLimitMock
)
})
})

describe('TopArtists', () => {
test('should get top artists', async () => {
vi.spyOn(statisticsService, 'topArtists').mockReturnValue(
of(spotifyResponseWithOffsetMockFactory(artistsMock))
vi.spyOn(statisticsService, 'topArtists').mockResolvedValue(
spotifyResponseWithOffsetMockFactory(artistsMock)
)

expect(
await firstValueFrom(statisticsController.topArtists('awd', {}))
).toEqual(spotifyResponseWithOffsetMockFactory(artistsMock))
expect(await statisticsController.topArtists('awd', {})).toEqual(
spotifyResponseWithOffsetMockFactory(artistsMock)
)
})

test('should get top artists with limit argument', async () => {
Expand All @@ -155,31 +148,25 @@ describe('StatisticsController', () => {
() => artistMock
)

vi.spyOn(statisticsService, 'topArtists').mockReturnValue(
of(spotifyResponseWithOffsetMockFactory(formattedArtistsWithLimitMock))
vi.spyOn(statisticsService, 'topArtists').mockResolvedValue(
spotifyResponseWithOffsetMockFactory(formattedArtistsWithLimitMock)
)

expect(
await firstValueFrom(statisticsController.topArtists('awd', { limit }))
).toEqual(
expect(await statisticsController.topArtists('awd', { limit })).toEqual(
spotifyResponseWithOffsetMockFactory(formattedArtistsWithLimitMock)
)
})
})

test('should get artist', async () => {
vi.spyOn(statisticsService, 'artist').mockReturnValue(of(artistMock))
vi.spyOn(statisticsService, 'artist').mockResolvedValue(artistMock)

expect(
await firstValueFrom(statisticsController.artist('awd', '123'))
).toEqual(artistMock)
expect(await statisticsController.artist('awd', '123')).toEqual(artistMock)
})

test('should get analysis', async () => {
vi.spyOn(statisticsService, 'analysis').mockReturnValue(of(analysisMock))
vi.spyOn(statisticsService, 'analysis').mockResolvedValue(analysisMock)

expect(await firstValueFrom(statisticsController.analysis('awd'))).toEqual(
analysisMock
)
expect(await statisticsController.analysis('awd')).toEqual(analysisMock)
})
})
6 changes: 2 additions & 4 deletions src/modules/statistics/statistics.service.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { HttpService } from '@nestjs/axios'
import { TestingModule, Test } from '@nestjs/testing'
import { firstValueFrom, of } from 'rxjs'
import { of } from 'rxjs'

import { StatisticsService } from './statistics.service'

Expand Down Expand Up @@ -109,9 +109,7 @@ describe('StatisticsService', () => {
of(axiosResponseMockFactory(spotifyArtistMock))
)

expect(
await firstValueFrom(statisticsService.artist('awd', 'some id'))
).toEqual(artistMock)
expect(await statisticsService.artist('awd', 'some id')).toEqual(artistMock)
})

test('should generate analysis', async () => {
Expand Down
22 changes: 12 additions & 10 deletions src/modules/statistics/statistics.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,16 +146,18 @@ export class StatisticsService {
}

artist(accessToken: string, id: string) {
return this.httpService
.get<SpotifyArtist>(
`/artists/${id}`,
applyAuthorizationHeader(accessToken)
)
.pipe(
map(response => response.data),
map(adaptArtist),
catchError(catchSpotifyError)
)
return firstValueFrom(
this.httpService
.get<SpotifyArtist>(
`/artists/${id}`,
applyAuthorizationHeader(accessToken)
)
.pipe(
map(response => response.data),
map(adaptArtist),
catchError(catchSpotifyError)
)
)
}

analysis(accessToken: string): Promise<Analysis> {
Expand Down
Loading

0 comments on commit 191d5ee

Please sign in to comment.