Skip to content

Commit

Permalink
refactor(modules/albums/service): use tracksService.findOrCreate me…
Browse files Browse the repository at this point in the history
…thod instead of `create`
  • Loading branch information
Mnigos committed Apr 22, 2024
1 parent 656a3b6 commit 2d18115
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 40 deletions.
21 changes: 10 additions & 11 deletions src/modules/albums/albums.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ describe('AlbumsService', () => {
provide: TracksService,
useValue: {
create: vi.fn(),
findOrCreate: vi.fn(),
},
},
{
Expand Down Expand Up @@ -193,7 +194,7 @@ describe('AlbumsService', () => {
describe('findOrCreateAlbumFromExternalId', () => {
let findAlbumByExternalIdSpy: MockInstance
let getAlbumSpy: GetAlbumMockInstance
let createTracksSpy: MockInstance
let findOrCreateSpy: MockInstance
let createSpy: MockInstance
let saveSpy: MockInstance

Expand All @@ -206,7 +207,7 @@ describe('AlbumsService', () => {
spotifyAlbumsService,
'getAlbum'
) as unknown as GetAlbumMockInstance
createTracksSpy = vi.spyOn(tracksService, 'create')
findOrCreateSpy = vi.spyOn(tracksService, 'findOrCreate')
createSpy = vi.spyOn(albumsService, 'create')
saveSpy = vi.spyOn(albumsRepository, 'save')
})
Expand All @@ -219,7 +220,7 @@ describe('AlbumsService', () => {

findAlbumByExternalIdSpy.mockResolvedValue(foundAlbumMock)
getAlbumSpy.mockResolvedValue(sdkAlbumMock)
createTracksSpy.mockResolvedValue(trackEntitiesMock)
findOrCreateSpy.mockResolvedValue(trackEntitiesMock)
createSpy.mockResolvedValue(albumEntityMock)
saveSpy.mockResolvedValue(foundAlbumMock)

Expand All @@ -228,9 +229,8 @@ describe('AlbumsService', () => {
).toEqual(foundAlbumMock)
expect(findAlbumByExternalIdSpy).toHaveBeenCalledWith(externalId)
expect(getAlbumSpy).toHaveBeenCalledWith(externalId, false)
expect(createTracksSpy).toHaveBeenCalledWith(
trackEntitiesMock.map(track => track.id),
[foundAlbumMock]
expect(findOrCreateSpy).toHaveBeenCalledWith(
trackEntitiesMock.map(track => track.id)
)
expect(saveSpy).toHaveBeenCalledWith(foundAlbumMock)
expect(createSpy).not.toHaveBeenCalled()
Expand Down Expand Up @@ -297,18 +297,17 @@ describe('AlbumsService', () => {
getAlbumsSpy.mockResolvedValue([sdkAlbumMock])
findAlbumsByExternalIdsSpy.mockResolvedValue([foundAlbumMock])
saveSpy.mockResolvedValue(foundAlbumMock)
const createTracksSpy = vi
.spyOn(tracksService, 'create')
const findOrCreateSpy = vi
.spyOn(tracksService, 'findOrCreate')
.mockResolvedValue(trackEntitiesMock)

expect(
await albumsService.findOrCreateAlbumsFromExternalIds(externalIds)
).toEqual([foundAlbumMock])
expect(getAlbumsSpy).toHaveBeenCalledWith(externalIds, false)
expect(findAlbumsByExternalIdsSpy).toHaveBeenCalledWith(externalIds)
expect(createTracksSpy).toHaveBeenCalledWith(
trackEntitiesMock.map(track => track.id),
[foundAlbumMock]
expect(findOrCreateSpy).toHaveBeenCalledWith(
trackEntitiesMock.map(track => track.id)
)
expect(saveSpy).toHaveBeenCalledWith(foundAlbumMock)
expect(createSpy).not.toHaveBeenCalled()
Expand Down
10 changes: 4 additions & 6 deletions src/modules/albums/albums.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,8 @@ export class AlbumsService {
)

if (foundAlbum?.tracks && foundAlbum.tracks.length > 0) {
const tracks = await this.tracksService.create(
albumToCreate.tracks.items.map(track => track.id),
[foundAlbum]
const tracks = await this.tracksService.findOrCreate(
albumToCreate.tracks.items.map(track => track.id)
)

foundAlbum.tracks = tracks
Expand Down Expand Up @@ -115,9 +114,8 @@ export class AlbumsService {
)

if (foundAlbum.tracks && foundAlbum.tracks.length > 0 && albumToCreate) {
const tracks = await this.tracksService.create(
albumToCreate.tracks.items.map(track => track.id),
[foundAlbum]
const tracks = await this.tracksService.findOrCreate(
albumToCreate.tracks.items.map(track => track.id)
)

const updateAlbumIndex = foundAlbums.findIndex(
Expand Down
43 changes: 20 additions & 23 deletions src/modules/tracks/tracks.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -239,45 +239,44 @@ describe('TracksService', () => {
})

describe('findOrCreateTracksFromExternalIds', () => {
const sdkTracks = [sdkTrackMock, sdkTrackMock]
const externalIds = sdkTracksMock.map(track => track.id)
const foundTracks = [trackEntityMock, trackEntityMock]

let findTrackByExternalIdSpy: MockInstance
let findOrCreateAlbum: MockInstance
let getTracksSpy: MockInstance

beforeEach(() => {
findTrackByExternalIdSpy = vi.spyOn(
tracksRepository,
'findTracksByExternalIds'
)
findOrCreateAlbum = vi.spyOn(albumsService, 'findOrCreate')
})

test('should return empty array if no tracks', async () => {
expect(await tracksService.findOrCreate([])).toEqual([])
expect(findTrackByExternalIdSpy).not.toHaveBeenCalled()
expect(findOrCreateAlbum).not.toHaveBeenCalled()
getTracksSpy = vi.spyOn(spotifyTracksService, 'getTracks')
})

test('should find tracks by external ids', async () => {
findTrackByExternalIdSpy.mockResolvedValue(foundTracks)

expect(await tracksService.findOrCreate(sdkTracks)).toEqual(foundTracks)
expect(findTrackByExternalIdSpy).toHaveBeenCalledWith(
sdkTracks.map(track => track.id)
)
expect(
await tracksService.findOrCreateTracksFromExternalIds(externalIds)
).toEqual(foundTracks)
expect(findTrackByExternalIdSpy).toHaveBeenCalledWith(externalIds)
expect(getTracksSpy).not.toHaveBeenCalled()
expect(findOrCreateAlbum).not.toHaveBeenCalled()
})

test('should create tracks from external ids', async () => {
findTrackByExternalIdSpy
.mockResolvedValueOnce([])
.mockResolvedValue(foundTracks)
getTracksSpy.mockResolvedValue(sdkTracksMock)

expect(await tracksService.findOrCreate(sdkTracks)).toEqual(foundTracks)
expect(findTrackByExternalIdSpy).toHaveBeenCalledWith(
sdkTracks.map(track => track.id)
)
expect(
await tracksService.findOrCreateTracksFromExternalIds(externalIds)
).toEqual(foundTracks)
expect(findTrackByExternalIdSpy).toHaveBeenCalledWith(externalIds)
expect(getTracksSpy).toHaveBeenCalledWith(externalIds, false)
expect(findTrackByExternalIdSpy).toHaveBeenCalledTimes(2)
})
})
Expand All @@ -297,16 +296,12 @@ describe('TracksService', () => {
findOrCreateAlbum = vi.spyOn(albumsService, 'findOrCreate')
})

test('should return empty array if no tracks', async () => {
expect(await tracksService.findOrCreate([])).toEqual([])
expect(findTrackByExternalIdSpy).not.toHaveBeenCalled()
expect(findOrCreateAlbum).not.toHaveBeenCalled()
})

test('should find tracks by external ids', async () => {
findTrackByExternalIdSpy.mockResolvedValue(foundTracks)

expect(await tracksService.findOrCreate(sdkTracks)).toEqual(foundTracks)
expect(await tracksService.findOrCreateTracksFromDtos(sdkTracks)).toEqual(
foundTracks
)
expect(findTrackByExternalIdSpy).toHaveBeenCalledWith(
sdkTracks.map(track => track.id)
)
Expand All @@ -318,7 +313,9 @@ describe('TracksService', () => {
.mockResolvedValueOnce([])
.mockResolvedValue(foundTracks)

expect(await tracksService.findOrCreate(sdkTracks)).toEqual(foundTracks)
expect(await tracksService.findOrCreateTracksFromDtos(sdkTracks)).toEqual(
foundTracks
)
expect(findTrackByExternalIdSpy).toHaveBeenCalledWith(
sdkTracks.map(track => track.id)
)
Expand Down

0 comments on commit 2d18115

Please sign in to comment.