aboutsummaryrefslogtreecommitdiffhomepage
path: root/client/src/app/videos/recommendations/recent-videos-recommendation.service.spec.ts
blob: 698b2e27b23f953ebd689daac825d86ddf1459b1 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import { RecentVideosRecommendationService } from '@app/videos/recommendations/recent-videos-recommendation.service'
import { VideosProvider } from '@app/shared/video/video.service'
import { EMPTY, of } from 'rxjs'
import Mock = jest.Mock

describe('"Recent Videos" Recommender', () => {
  describe('getRecommendations', () => {
    let videosService: VideosProvider
    let service: RecentVideosRecommendationService
    let getVideosMock: Mock<any>
    beforeEach(() => {
      getVideosMock = jest.fn(() => EMPTY)
      videosService = {
        getVideos: getVideosMock
      }
      service = new RecentVideosRecommendationService(videosService)
    })
    it('should filter out the given UUID from the results', async (done) => {
      const vids = [
        { uuid: 'uuid1' },
        { uuid: 'uuid2' }
      ]
      getVideosMock.mockReturnValueOnce(of({ videos: vids }))
      const result = await service.getRecommendations({ uuid: 'uuid1' }).toPromise()
      const uuids = result.map(v => v.uuid)
      expect(uuids).toEqual(['uuid2'])
      done()
    })
    it('should return 5 results when the given UUID is NOT in the first 5 results', async (done) => {
      const vids = [
        { uuid: 'uuid2' },
        { uuid: 'uuid3' },
        { uuid: 'uuid4' },
        { uuid: 'uuid5' },
        { uuid: 'uuid6' },
        { uuid: 'uuid7' }
      ]
      getVideosMock.mockReturnValueOnce(of({ videos: vids }))
      const result = await service.getRecommendations({ uuid: 'uuid1' }).toPromise()
      expect(result.length).toEqual(5)
      done()
    })
    it('should return 5 results when the given UUID IS PRESENT in the first 5 results', async (done) => {
      const vids = [
        { uuid: 'uuid1' },
        { uuid: 'uuid2' },
        { uuid: 'uuid3' },
        { uuid: 'uuid4' },
        { uuid: 'uuid5' },
        { uuid: 'uuid6' }
      ]
      getVideosMock
        .mockReturnValueOnce(of({ videos: vids }))
      const result = await service.getRecommendations({ uuid: 'uuid1' }).toPromise()
      expect(result.length).toEqual(5)
      done()
    })
    it('should fetch an extra result in case the given UUID is in the list', async (done) => {
      await service.getRecommendations({ uuid: 'uuid1' }).toPromise()
      let expectedSize = service.pageSize + 1
      let params = { currentPage: jasmine.anything(), itemsPerPage: expectedSize }
      expect(getVideosMock).toHaveBeenCalledWith(params, jasmine.anything())
      done()
    })
  })
})