diff --git a/package.json b/package.json index 76673bf7..22d82d5c 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/modules/player/player.controller.spec.ts b/src/modules/player/player.controller.spec.ts index 4cad2848..452753a7 100644 --- a/src/modules/player/player.controller.spec.ts +++ b/src/modules/player/player.controller.spec.ts @@ -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' @@ -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, }) }) diff --git a/src/modules/statistics/statistics.controller.spec.ts b/src/modules/statistics/statistics.controller.spec.ts index 14ddffc2..7b5a7dfd 100644 --- a/src/modules/statistics/statistics.controller.spec.ts +++ b/src/modules/statistics/statistics.controller.spec.ts @@ -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' @@ -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 () => { @@ -65,13 +64,11 @@ 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) ) }) @@ -79,13 +76,13 @@ describe('StatisticsController', () => { 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 () => { @@ -96,13 +93,11 @@ 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) ) }) @@ -110,13 +105,11 @@ describe('StatisticsController', () => { 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 () => { @@ -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 () => { @@ -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) }) }) diff --git a/src/modules/statistics/statistics.service.spec.ts b/src/modules/statistics/statistics.service.spec.ts index ab41f236..1f12cd5d 100644 --- a/src/modules/statistics/statistics.service.spec.ts +++ b/src/modules/statistics/statistics.service.spec.ts @@ -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' @@ -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 () => { diff --git a/src/modules/statistics/statistics.service.ts b/src/modules/statistics/statistics.service.ts index a928887c..a1bf536e 100644 --- a/src/modules/statistics/statistics.service.ts +++ b/src/modules/statistics/statistics.service.ts @@ -146,16 +146,18 @@ export class StatisticsService { } artist(accessToken: string, id: string) { - return this.httpService - .get( - `/artists/${id}`, - applyAuthorizationHeader(accessToken) - ) - .pipe( - map(response => response.data), - map(adaptArtist), - catchError(catchSpotifyError) - ) + return firstValueFrom( + this.httpService + .get( + `/artists/${id}`, + applyAuthorizationHeader(accessToken) + ) + .pipe( + map(response => response.data), + map(adaptArtist), + catchError(catchSpotifyError) + ) + ) } analysis(accessToken: string): Promise { diff --git a/src/modules/users/users-profile.controller.spec.ts b/src/modules/users/users-profile.controller.spec.ts index 89789e70..75737676 100644 --- a/src/modules/users/users-profile.controller.spec.ts +++ b/src/modules/users/users-profile.controller.spec.ts @@ -1,5 +1,4 @@ import { Test } from '@nestjs/testing' -import { of } from 'rxjs' import { UsersProfileController } from './users-profile.controller' import { UsersRepository } from './users.repository' @@ -84,10 +83,10 @@ describe('UsersProfileController', () => { .mockResolvedValue(userMock) const tokenSpy = vi .spyOn(authService, 'token') - .mockReturnValue(of(secretDataMock)) + .mockResolvedValue(secretDataMock) const lastTracksSpy = vi .spyOn(statisticsService, 'lastTracks') - .mockReturnValue(of(spotifyResponseWithCursorsMockFactory(tracksMock))) + .mockResolvedValue(spotifyResponseWithCursorsMockFactory(tracksMock)) expect(await usersProfileController.getLastTracks(id, {})).toEqual( spotifyResponseWithCursorsMockFactory(tracksMock) @@ -117,10 +116,10 @@ describe('UsersProfileController', () => { .mockResolvedValue(userMock) const tokenSpy = vi .spyOn(authService, 'token') - .mockReturnValue(of(secretDataMock)) + .mockResolvedValue(secretDataMock) const lastTracksSpy = vi .spyOn(statisticsService, 'lastTracks') - .mockReturnValue(of(spotifyResponseWithCursorsMockFactory(tracksMock))) + .mockResolvedValue(spotifyResponseWithCursorsMockFactory(tracksMock)) expect( await usersProfileController.getLastTracks(id, { @@ -147,10 +146,10 @@ describe('UsersProfileController', () => { .mockResolvedValue(userMock) const tokenSpy = vi .spyOn(authService, 'token') - .mockReturnValue(of(secretDataMock)) + .mockResolvedValue(secretDataMock) const topGenresSpy = vi .spyOn(statisticsService, 'topGenres') - .mockReturnValue(of(topGenresMock)) + .mockResolvedValue(topGenresMock) expect(await usersProfileController.getTopGenres(id, {})).toEqual( topGenresMock @@ -180,10 +179,10 @@ describe('UsersProfileController', () => { .mockResolvedValue(userMock) const tokenSpy = vi .spyOn(authService, 'token') - .mockReturnValue(of(secretDataMock)) + .mockResolvedValue(secretDataMock) const topGenresSpy = vi .spyOn(statisticsService, 'topGenres') - .mockReturnValue(of(topGenresMock)) + .mockResolvedValue(topGenresMock) expect( await usersProfileController.getTopGenres(id, { @@ -210,10 +209,10 @@ describe('UsersProfileController', () => { .mockResolvedValue(userMock) const tokenSpy = vi .spyOn(authService, 'token') - .mockReturnValue(of(secretDataMock)) + .mockResolvedValue(secretDataMock) const topArtistsSpy = vi .spyOn(statisticsService, 'topArtists') - .mockReturnValue(of(spotifyResponseWithOffsetMockFactory(artistsMock))) + .mockResolvedValue(spotifyResponseWithOffsetMockFactory(artistsMock)) expect(await usersProfileController.getTopArtists(id, {})).toEqual( spotifyResponseWithOffsetMockFactory(artistsMock) @@ -243,10 +242,10 @@ describe('UsersProfileController', () => { .mockResolvedValue(userMock) const tokenSpy = vi .spyOn(authService, 'token') - .mockReturnValue(of(secretDataMock)) + .mockResolvedValue(secretDataMock) const topArtistsSpy = vi .spyOn(statisticsService, 'topArtists') - .mockReturnValue(of(spotifyResponseWithOffsetMockFactory(artistsMock))) + .mockResolvedValue(spotifyResponseWithOffsetMockFactory(artistsMock)) expect( await usersProfileController.getTopArtists(id, { @@ -273,10 +272,10 @@ describe('UsersProfileController', () => { .mockResolvedValue(userMock) const tokenSpy = vi .spyOn(authService, 'token') - .mockReturnValue(of(secretDataMock)) + .mockResolvedValue(secretDataMock) const topTracksSpy = vi .spyOn(statisticsService, 'topTracks') - .mockReturnValue(of(spotifyResponseWithOffsetMockFactory(tracksMock))) + .mockResolvedValue(spotifyResponseWithOffsetMockFactory(tracksMock)) expect(await usersProfileController.getTopTracks(id, {})).toEqual( spotifyResponseWithOffsetMockFactory(tracksMock) @@ -306,10 +305,10 @@ describe('UsersProfileController', () => { .mockResolvedValue(userMock) const tokenSpy = vi .spyOn(authService, 'token') - .mockReturnValue(of(secretDataMock)) + .mockResolvedValue(secretDataMock) const topTracksSpy = vi .spyOn(statisticsService, 'topTracks') - .mockReturnValue(of(spotifyResponseWithOffsetMockFactory(tracksMock))) + .mockResolvedValue(spotifyResponseWithOffsetMockFactory(tracksMock)) expect( await usersProfileController.getTopTracks(id, { @@ -336,10 +335,10 @@ describe('UsersProfileController', () => { .mockResolvedValue(userMock) const tokenSpy = vi .spyOn(authService, 'token') - .mockReturnValue(of(secretDataMock)) + .mockResolvedValue(secretDataMock) const analysisSpy = vi .spyOn(statisticsService, 'analysis') - .mockReturnValue(of(analysisMock)) + .mockResolvedValue(analysisMock) expect(await usersProfileController.getAnalysis(id)).toEqual(analysisMock) expect(tokenSpy).toHaveBeenCalledWith({ diff --git a/src/modules/users/users-profile.controller.ts b/src/modules/users/users-profile.controller.ts index 313b7f34..b9ad011e 100644 --- a/src/modules/users/users-profile.controller.ts +++ b/src/modules/users/users-profile.controller.ts @@ -16,7 +16,6 @@ import { ApiBadRequestResponse, ApiTags, } from '@nestjs/swagger' -import { firstValueFrom } from 'rxjs' import { UsersRepository } from './users.repository' import { USER } from './users.controller' @@ -67,15 +66,11 @@ export class UsersProfileController { if (!foundUser) throw new NotFoundException(NOT_BEEN_FOUND(USER)) - const { accessToken } = await firstValueFrom( - this.authService.token({ - refreshToken: foundUser.refreshToken, - }) - ) + const { accessToken } = await this.authService.token({ + refreshToken: foundUser.refreshToken, + }) - return firstValueFrom( - this.statisticsService.lastTracks(accessToken, limit, before, after) - ) + return this.statisticsService.lastTracks(accessToken, limit, before, after) } @Get('top/artists') @@ -101,14 +96,15 @@ export class UsersProfileController { if (!foundUser) throw new NotFoundException(NOT_BEEN_FOUND(USER)) - const { accessToken } = await firstValueFrom( - this.authService.token({ - refreshToken: foundUser.refreshToken, - }) - ) + const { accessToken } = await this.authService.token({ + refreshToken: foundUser.refreshToken, + }) - return firstValueFrom( - this.statisticsService.topArtists(accessToken, limit, timeRange, offset) + return this.statisticsService.topArtists( + accessToken, + limit, + timeRange, + offset ) } @@ -135,14 +131,15 @@ export class UsersProfileController { if (!foundUser) throw new NotFoundException(NOT_BEEN_FOUND(USER)) - const { accessToken } = await firstValueFrom( - this.authService.token({ - refreshToken: foundUser.refreshToken, - }) - ) + const { accessToken } = await this.authService.token({ + refreshToken: foundUser.refreshToken, + }) - return firstValueFrom( - this.statisticsService.topTracks(accessToken, limit, timeRange, offset) + return this.statisticsService.topTracks( + accessToken, + limit, + timeRange, + offset ) } @@ -169,14 +166,15 @@ export class UsersProfileController { if (!foundUser) throw new NotFoundException(NOT_BEEN_FOUND(USER)) - const { accessToken } = await firstValueFrom( - this.authService.token({ - refreshToken: foundUser.refreshToken, - }) - ) + const { accessToken } = await this.authService.token({ + refreshToken: foundUser.refreshToken, + }) - return firstValueFrom( - this.statisticsService.topGenres(accessToken, limit, timeRange, offset) + return this.statisticsService.topGenres( + accessToken, + limit, + timeRange, + offset ) } @@ -199,12 +197,10 @@ export class UsersProfileController { if (!foundUser) throw new NotFoundException(NOT_BEEN_FOUND(USER)) - const { accessToken } = await firstValueFrom( - this.authService.token({ - refreshToken: foundUser.refreshToken, - }) - ) + const { accessToken } = await this.authService.token({ + refreshToken: foundUser.refreshToken, + }) - return firstValueFrom(this.statisticsService.analysis(accessToken)) + return this.statisticsService.analysis(accessToken) } } diff --git a/yarn.lock b/yarn.lock index eb54e771..1b7ca6cc 100644 --- a/yarn.lock +++ b/yarn.lock @@ -665,10 +665,10 @@ "@types/jsonwebtoken" "9.0.5" jsonwebtoken "9.0.2" -"@nestjs/mapped-types@2.0.3": - version "2.0.3" - resolved "https://registry.yarnpkg.com/@nestjs/mapped-types/-/mapped-types-2.0.3.tgz#f400432da6c98d02b94b14e893fd7fa46c152403" - integrity sha512-40Zdqg98lqoF0+7ThWIZFStxgzisK6GG22+1ABO4kZiGF/Tu2FE+DYLw+Q9D94vcFWizJ+MSjNN4ns9r6hIGxw== +"@nestjs/mapped-types@2.0.4": + version "2.0.4" + resolved "https://registry.yarnpkg.com/@nestjs/mapped-types/-/mapped-types-2.0.4.tgz#97280b06bf85d34ea9bad1e847e5e3cbaca8f04f" + integrity sha512-xl+gUSp0B+ln1VSNoUftlglk8dfpUes3DHGxKZ5knuBxS5g2H/8p9/DSBOYWUfO5f4u9s6ffBPZ71WO+tbe5SA== "@nestjs/passport@10.0.3": version "10.0.3" @@ -708,16 +708,16 @@ jsonc-parser "3.2.0" pluralize "8.0.0" -"@nestjs/swagger@7.1.16": - version "7.1.16" - resolved "https://registry.yarnpkg.com/@nestjs/swagger/-/swagger-7.1.16.tgz#110dac7f2ae9bbf04943fba51df4fa4b47a66ef3" - integrity sha512-f9KBk/BX9MUKPTj7tQNYJ124wV/jP5W2lwWHLGwe/4qQXixuDOo39zP55HIJ44LE7S04B7BOeUOo9GBJD/vRcw== +"@nestjs/swagger@7.1.17": + version "7.1.17" + resolved "https://registry.yarnpkg.com/@nestjs/swagger/-/swagger-7.1.17.tgz#3f8d849db8983cf609264270e83418bf5b379085" + integrity sha512-ASCxBrvMEN2o/8vEEmrIPMNzrr/hVi7QIR4y1oNYvoBNXHuwoF1VSI3+4Rq/3xmwVnVveJxHlBIs2u5xY9VgGQ== dependencies: - "@nestjs/mapped-types" "2.0.3" + "@nestjs/mapped-types" "2.0.4" js-yaml "4.1.0" lodash "4.17.21" path-to-regexp "3.2.0" - swagger-ui-dist "5.9.1" + swagger-ui-dist "5.10.3" "@nestjs/testing@^10.2.10": version "10.2.10" @@ -6583,10 +6583,10 @@ supports-preserve-symlinks-flag@^1.0.0: resolved "https://registry.yarnpkg.com/supports-preserve-symlinks-flag/-/supports-preserve-symlinks-flag-1.0.0.tgz#6eda4bd344a3c94aea376d4cc31bc77311039e09" integrity sha512-ot0WnXS9fgdkgIcePe6RHNk1WA8+muPa6cSjeR3V8K27q9BB1rTE3R1p7Hv0z1ZyAc8s6Vvv8DIyWf681MAt0w== -swagger-ui-dist@5.9.1: - version "5.9.1" - resolved "https://registry.yarnpkg.com/swagger-ui-dist/-/swagger-ui-dist-5.9.1.tgz#d0bcd614e3752da02df141846348f84468ae815e" - integrity sha512-5zAx+hUwJb9T3EAntc7TqYkV716CMqG6sZpNlAAMOMWkNXRYxGkN8ADIvD55dQZ10LxN90ZM/TQmN7y1gpICnw== +swagger-ui-dist@5.10.3: + version "5.10.3" + resolved "https://registry.yarnpkg.com/swagger-ui-dist/-/swagger-ui-dist-5.10.3.tgz#903adbfbecc0670a802b6d8b770e5dd07b5a36cb" + integrity sha512-fu3aozjxFWsmcO1vyt1q1Ji2kN7KlTd1vHy27E9WgPyXo9nrEzhQPqgxaAjbMsOmb8XFKNGo4Sa3Q+84Fh+pFw== symbol-observable@4.0.0: version "4.0.0"