Created
November 21, 2017 07:44
-
-
Save Ugmaxie/7bc134a42978e41f276a9f90a5aa6e59 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { async, ComponentFixture, TestBed } from '@angular/core/testing'; | |
import { DownloadPdfReportComponent } from './download-pdf-report.component'; | |
import { RouterTestingModule } from '@angular/router/testing'; | |
import { ActivatedRoute, Router } from '@angular/router'; | |
import { NO_ERRORS_SCHEMA } from '@angular/core'; | |
import { Observable } from 'rxjs/Observable'; | |
import { Subject } from 'rxjs/Subject'; | |
import 'rxjs/add/observable/of'; | |
import { MedianService } from '@cb/tdp-core/services'; | |
import { | |
AnalyticsLoggingServiceMock, | |
AngularticsTestingModule, | |
CbCommonTestingModule, | |
HttpTestingModule, | |
LoggingServiceMock | |
} from '@cb/tdp-core/testing'; | |
import { AnalyticsLoggingService, FormattedNumber, LoggingService } from '@cb/tdp-core'; | |
import { | |
DownloadAnalyticsReportService | |
} from '@cb/tdp-core/services/download-analytics-report/download-analytics-report.service'; | |
import { AnalyticsServicesTestingModule } from '../../../testing/analytics-services.module.mock'; | |
import { DownloadAnalyticsReportServiceMock } from '../../../testing/mocks/download-analytics-report.service.mock'; | |
describe('Component Analytics: DownloadPdfReportComponent', () => { | |
let fixture: ComponentFixture<DownloadPdfReportComponent>; | |
let context: DownloadPdfReportComponent; | |
let loggingService: LoggingServiceMock; | |
let activatedRoute: ActivatedRouteServiceMock; | |
let downloadAnalyticsReportService: DownloadAnalyticsReportServiceMock; | |
let analyticsLoggingService: AnalyticsLoggingServiceMock; | |
beforeEach(async(() => { | |
TestBed.configureTestingModule({ | |
declarations: [DownloadPdfReportComponent], | |
schemas: [NO_ERRORS_SCHEMA], | |
providers: [ | |
{ | |
provide: ActivatedRoute, | |
useClass: ActivatedRouteServiceMock | |
}, | |
MedianService, | |
DownloadAnalyticsReportService, | |
FormattedNumber | |
], | |
imports: [ | |
AngularticsTestingModule, | |
HttpTestingModule, | |
RouterTestingModule, | |
CbCommonTestingModule, | |
AnalyticsServicesTestingModule | |
] | |
}); | |
const router = TestBed.get(Router); | |
spyOn(router, 'navigate').and.stub(); | |
loggingService = TestBed.get(LoggingService); | |
activatedRoute = TestBed.get(ActivatedRoute); | |
analyticsLoggingService = TestBed.get(AnalyticsLoggingService); | |
downloadAnalyticsReportService = TestBed.get(DownloadAnalyticsReportService); | |
fixture = TestBed.createComponent(DownloadPdfReportComponent); | |
context = fixture.componentInstance; | |
})); | |
it('logDownload check trigger TrackEvent method', () => { | |
const reportId = 'full_report'; | |
spyOn(loggingService, 'logEvent').and.callThrough(); | |
spyOn(analyticsLoggingService, 'TrackEvent').and.returnValue(new Subject()); | |
fixture.detectChanges(); | |
context.logDownload(reportId); | |
expect(analyticsLoggingService.TrackEvent).toHaveBeenCalled(); | |
}); | |
it('unlockAnalyticsPDFReport: unlock full_report', () => { | |
const reportIdMock = 'full_report'; | |
context.isDownloadSummaryReportLocked = true; | |
context.isDownloadFullReportLocked = true; | |
spyOn(downloadAnalyticsReportService, 'isSummaryReportLocked').and.returnValue(false); | |
spyOn(downloadAnalyticsReportService, 'isFullReportLocked').and.returnValue(true); | |
context.unlockAnalyticsPDFReport(reportIdMock); | |
expect(context.isDownloadFullReportLocked).toBeFalsy(); | |
expect(context.isDownloadSummaryReportLocked).toBeTruthy(); | |
}); | |
it('unlockAnalyticsPDFReport: unlock summary_report', () => { | |
const reportIdMock = 'summary_report'; | |
context.isDownloadSummaryReportLocked = true; | |
context.isDownloadFullReportLocked = true; | |
spyOn(downloadAnalyticsReportService, 'isSummaryReportLocked').and.returnValue(true); | |
spyOn(downloadAnalyticsReportService, 'isFullReportLocked').and.returnValue(false); | |
context.unlockAnalyticsPDFReport(reportIdMock); | |
expect(context.isDownloadFullReportLocked).toBeTruthy(); | |
expect(context.isDownloadSummaryReportLocked).toBeFalsy(); | |
}); | |
it('check method downloadAnalyticsReport when report locked', () => { | |
const reportIdMock = 'summary_report'; | |
context.isDownloadSummaryReportLocked = true; | |
context.isDownloadFullReportLocked = true; | |
context.processedSearchResult = { | |
SearchParameters: { | |
keywords: 'keywords' | |
}, | |
LocationSearched: 'LocationSearched' | |
}; | |
spyOn(downloadAnalyticsReportService, 'send').and.returnValue(Observable.of()); | |
spyOn(downloadAnalyticsReportService, 'saveFile').and.stub(); | |
spyOn(downloadAnalyticsReportService, 'isSummaryReportLocked').and.returnValue(true); | |
spyOn(downloadAnalyticsReportService, 'isFullReportLocked').and.returnValue(false); | |
spyOn(downloadAnalyticsReportService, 'getDownloadFileName').and.returnValue('summary-report.pdf'); | |
spyOn(context, 'logDownload').and.stub(); | |
context.downloadAnalyticsReport(reportIdMock); | |
expect(downloadAnalyticsReportService.isFullReportLocked).toHaveBeenCalled(); | |
expect(downloadAnalyticsReportService.isSummaryReportLocked).toHaveBeenCalled(); | |
expect(downloadAnalyticsReportService.send).toHaveBeenCalled(); | |
expect(context.isDownloadSummaryReportLocked).toBeTruthy(); | |
expect(context.isDownloadFullReportLocked).toBeFalsy(); | |
}); | |
it('check method downloadAnalyticsReport when report unlocked', () => { | |
const reportIdMock = 'summary_report'; | |
context.processedSearchResult = { | |
SearchParameters: { | |
keywords: 'keywords' | |
}, | |
LocationSearched: 'LocationSearched' | |
}; | |
spyOn(downloadAnalyticsReportService, 'send').and.returnValue(Observable.of({data: '123'})); | |
spyOn(downloadAnalyticsReportService, 'saveFile').and.stub(); | |
spyOn(downloadAnalyticsReportService, 'isSummaryReportLocked').and.returnValue(true); | |
spyOn(downloadAnalyticsReportService, 'isFullReportLocked').and.returnValue(false); | |
spyOn(downloadAnalyticsReportService, 'getDownloadFileName').and.returnValue('summary-report.pdf'); | |
spyOn(context, 'logDownload').and.stub(); | |
context.downloadAnalyticsReport(reportIdMock); | |
expect(downloadAnalyticsReportService.isFullReportLocked).toHaveBeenCalled(); | |
expect(downloadAnalyticsReportService.isSummaryReportLocked).toHaveBeenCalled(); | |
expect(downloadAnalyticsReportService.send).toHaveBeenCalled(); | |
expect(context.isDownloadSummaryReportLocked).toBeFalsy(); | |
expect(context.isDownloadSummaryReportLocked).toBeFalsy(); | |
}); | |
it('check method downloadAnalyticsReport when report unlocked', () => { | |
const reportIdMock = 'summary_report'; | |
context.processedSearchResult = { | |
SearchParameters: { | |
keywords: 'keywords' | |
}, | |
LocationSearched: 'LocationSearched' | |
}; | |
spyOn(downloadAnalyticsReportService, 'send').and.returnValue(Observable.throw(new Error('test error!!!'))); | |
spyOn(downloadAnalyticsReportService, 'isSummaryReportLocked').and.returnValue(true); | |
spyOn(downloadAnalyticsReportService, 'isFullReportLocked').and.returnValue(false); | |
spyOn(downloadAnalyticsReportService, 'getDownloadFileName').and.returnValue('summary-report.pdf'); | |
spyOn(context, 'logDownload').and.stub(); | |
context.downloadAnalyticsReport(reportIdMock); | |
expect(downloadAnalyticsReportService.isFullReportLocked).toHaveBeenCalled(); | |
expect(downloadAnalyticsReportService.isSummaryReportLocked).toHaveBeenCalled(); | |
expect(downloadAnalyticsReportService.send).toHaveBeenCalled(); | |
expect(context.isDownloadSummaryReportLocked).toBeFalsy(); | |
expect(context.isDownloadSummaryReportLocked).toBeFalsy(); | |
}); | |
it('check method ngOnChanges', () => { | |
context.processedSearchResult = { | |
SearchParameters: {} | |
}; | |
spyOn(context, 'checkDataConsistency'); | |
const dataInput = context.processedSearchResult; | |
context.ngOnChanges(); | |
expect(context.checkDataConsistency).toHaveBeenCalledWith(dataInput); | |
}); | |
it('checkDataConsistency() check if branch when appropriate dataInput', () => { | |
spyOn(context, 'checkSupplyInconsistency').and.returnValue(true); | |
spyOn(context, 'checkDemandInconsistency').and.stub(); | |
spyOn(context, 'checkCompensationInconsistency').and.stub(); | |
context.processedSearchResult = { | |
Compensation: { | |
Breakout: 'breakout mock' | |
}, | |
MarketRankingData: { | |
ranked_items: ['item1', 'item2'] | |
}, | |
Gateway: { | |
Supply: {}, | |
Demand: {} | |
}, | |
SearchParameters: { | |
keywords: 'test', | |
location: { | |
city_state: 'test_city' | |
} | |
}, | |
LocationSearchedType: 'testType' | |
}; | |
context.checkDataConsistency(context.processedSearchResult); | |
expect(context.checkSupplyInconsistency).toHaveBeenCalled(); | |
expect(context.checkDemandInconsistency).toHaveBeenCalled(); | |
expect(context.checkCompensationInconsistency).toHaveBeenCalled(); | |
}); | |
it('checkDataConsistency() check branch else when not appropriate dataInput', () => { | |
spyOn(context, 'checkSupplyInconsistency').and.returnValue(true); | |
spyOn(context, 'checkDemandInconsistency').and.stub(); | |
spyOn(context, 'checkCompensationInconsistency').and.stub(); | |
context.processedSearchResult = { | |
Compensation: { | |
Breakout: 'breakout mock' | |
}, | |
MarketRankingData: {}, | |
Gateway: { | |
Supply: {}, | |
Demand: {} | |
}, | |
SearchParameters: { | |
keywords: 'test', | |
location: { | |
city_state: 'test_city' | |
} | |
}, | |
LocationSearchedType: 'testType' | |
}; | |
context.checkDataConsistency(context.processedSearchResult); | |
expect(context.checkSupplyInconsistency).not.toHaveBeenCalled(); | |
expect(context.checkDemandInconsistency).not.toHaveBeenCalled(); | |
expect(context.checkCompensationInconsistency).not.toHaveBeenCalled(); | |
}); | |
it('checkCompensationInconsistency check compensationDataset and return true', () => { | |
const compensationDataset: MockCompensationDataset = { | |
compensationSet: { | |
Compensation: { | |
Breakout: { | |
Data: [{ | |
Percentile50: { | |
Pay: 1 | |
} | |
}] | |
} | |
}, | |
NationalCompensation: { | |
Breakout: { | |
Data: [{ | |
Percentile50: { | |
Pay: 1 | |
} | |
}] | |
} | |
}, | |
Breakout: { | |
Data: [{ | |
Percentile50: { | |
Pay: 1 | |
} | |
}] | |
} | |
}, | |
compensationBreakoutSet: { | |
CompensationBreakoutData: { | |
experience: true | |
} | |
} | |
}; | |
const result = context.checkCompensationInconsistency(compensationDataset); | |
expect(result).toBeTruthy(); | |
}); | |
it('checkCompensationInconsistency check demandDataset and return true', () => { | |
const demandDataset = { | |
TotalResults: 3, | |
TimeFrameEnd: {} | |
}; | |
const result = context.checkDemandInconsistency(demandDataset); | |
expect(result).toBeTruthy(); | |
}); | |
it('checkSupplyInconsistency check supplyDataset and return true', () => { | |
const supplyDataset = { | |
TotalResults: 3, | |
YearsOfExperience: [{}, {}], | |
EducationLevels: [{}, {}], | |
TopCityStates: [{}, {}], | |
RecentJobTitles: [{}], | |
TopEmployerCompanies: {} | |
}; | |
fixture.detectChanges(); | |
const result = context.checkSupplyInconsistency(supplyDataset); | |
expect(result).toBeTruthy(); | |
}); | |
}); | |
interface MockCompensationDataset { | |
compensationSet: any; | |
compensationBreakoutSet: any; | |
} | |
class ActivatedRouteServiceMock { | |
params = { | |
_value:{ | |
searchId: 123 | |
} | |
}; | |
parent = { | |
snapshot: { | |
url: [ | |
{ path: '/path' } | |
] | |
} | |
}; | |
component = { | |
name: 'test' | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment