import { NewsfeedService } from './newsfeed.service' ; import { clientMock } from '../../../../tests/client-mock.spec' ; import { sessionMock } from '../../../../tests/session-mock.spec' ; import { fakeAsync , TestBed } from '@angular/core/testing' ; import { NSFWSelectorComponent } from '../../../common/components/nsfw-selector/nsfw-selector.component' ; import { MockService } from '../../../utils/mock' ; import { NSFWSelectorConsumerService } from '../../../common/components/nsfw-selector/nsfw-selector.service' ; import { analyticsServiceMock } from '../../../../tests/analytics-service-mock.spec' ; import { Client } from '../../../services/api' ; import { Session } from '../../../services/session' ; import { AnalyticsService } from '../../../services/analytics' ; import { HttpClientTestingModule , HttpTestingController , } from '@angular/common/http/testing' ; import { PLATFORM_ID } from '@angular/core' ; describe ( 'NewsfeedService' , ( ) => { let service ; let NSFWSelectorServiceMock = MockService ( NSFWSelectorConsumerService , { } ) ; beforeEach ( ( ) => { jasmine . clock ( ) . uninstall ( ) ; jasmine . clock ( ) . install ( ) ; TestBed . configureTestingModule ( { imports : [ HttpClientTestingModule ] , providers : [ { provide : Client , useValue : clientMock , } , { provide : Session , useValue : sessionMock , } , { provide : NSFWSelectorConsumerService , useValue : NSFWSelectorServiceMock , } , { provide : AnalyticsService , useValue : analyticsServiceMock , } , NewsfeedService , { provide : PLATFORM_ID , useValue : 'server' , } , ] , } ) ; service = TestBed . inject ( NewsfeedService ) ; clientMock . response = { } ; } ) ; afterEach ( ( ) => { jasmine . clock ( ) . uninstall ( ) ; } ) ; it ( 'should be instantiated' , ( ) => { expect ( service ) . toBeTruthy ( ) ; } ) ; it ( 'should record an activity view in newsfeed' , fakeAsync ( ( ) => { const url = 'api/v2/analytics/views/activity/123' ; clientMock . response [ url ] = { status : 'success' } ; const entity = { guid : 123 , } ; service . recordView ( entity ) ; jasmine . clock ( ) . tick ( 10 ) ; expect ( clientMock . post ) . toHaveBeenCalled ( ) ; expect ( clientMock . post . calls . mostRecent ( ) . args [ 0 ] ) . toContain ( url ) ; } ) ) ; it ( 'should record a boosted activity view in the newsfeed' , fakeAsync ( ( ) => { const url = 'api/v2/analytics/views/boost/1234' ; clientMock . response [ url ] = { status : 'success' } ; const entity = { guid : 123 , boosted_guid : 1234 , } ; service . recordView ( entity ) ; jasmine . clock ( ) . tick ( 10 ) ; expect ( clientMock . post ) . toHaveBeenCalled ( ) ; expect ( clientMock . post . calls . mostRecent ( ) . args [ 0 ] ) . toContain ( url ) ; } ) ) ; it ( 'should record a boosted activity view stop in the newsfeed' , fakeAsync ( ( ) => { const url = 'api/v2/analytics/views/boost/1234/stop' ; clientMock . response [ url ] = { status : 'success' } ; const entity = { guid : 123 , boosted_guid : 1234 , } ; service . recordView ( entity , false ) ; jasmine . clock ( ) . tick ( 10 ) ; expect ( clientMock . post ) . toHaveBeenCalled ( ) ; expect ( clientMock . post . calls . mostRecent ( ) . args [ 0 ] ) . toContain ( url ) ; } ) ) ; it ( 'should record a boosted activity view in a channel' , fakeAsync ( ( ) => { const url = 'api/v2/analytics/views/boost/1234/456' ; clientMock . response [ url ] = { status : 'success' } ; const entity = { guid : 123 , boosted_guid : 1234 , } ; const channel = { guid : '456' , } ; service . recordView ( entity , true , channel ) ; jasmine . clock ( ) . tick ( 10 ) ; expect ( clientMock . post ) . toHaveBeenCalled ( ) ; expect ( clientMock . post . calls . mostRecent ( ) . args [ 0 ] ) . toContain ( url ) ; } ) ) ; it ( 'should record a boosted activity view stop in a channel' , ( ) => { const url = 'api/v2/analytics/views/boost/1234/456/stop' ; clientMock . response [ url ] = { status : 'success' } ; const entity = { guid : 123 , boosted_guid : 1234 , } ; const channel = { guid : '456' , } ; service . recordView ( entity , false , channel ) ; jasmine . clock ( ) . tick ( 10 ) ; expect ( clientMock . post ) . toHaveBeenCalled ( ) ; expect ( clientMock . post . calls . mostRecent ( ) . args [ 0 ] ) . toContain ( url ) ; } ) ; } ) ;