I have a testing component and few tests that require different mocks to be used by spectator. The problem is I couldn't get specatator to change them dynamicly as the test go on.
Basicly I have 2 mocks that are imported from .json files, one being the right one and the other one is the one with error.
Correct: profitability-customer.json Invalid: profitability-customer-JSON-error-1.json
On test 8 I need spectator to switch mock to invalid one.
Here is what I have:
import * as reportMock from '@app/__mocks__/reports/profitability-customer.json';
import * as reportMockError from '@app/__mocks__/reports/profitability-customer-JSON-error-1.json';
let spectator: Spectator<ProfitabilityCustomerReportComponent>;
beforeEach(() => {
spectator = createComponent();
spectator.component.reportForm.controls.brands.setValue([testCustomer.brands[1].bkId]);
testIter++;
});
createComponent = createComponentFactory({
component: ProfitabilityCustomerReportComponent,
imports: [SharedModule, HttpClientTestingModule, RouterTestingModule],
providers: [MapService, DateSelectService,
mockProvider(HeaderCustomerService, HeaderCustomerServiceMock),
mockProvider(MapService, MapServiceMock),
mockProvider(ReportingApiService, myFOO())
],
shallow: false
});
function myFOO() {
let ReportingApiServiceMock: any;
console.log(testIter);
if (testIter === 8) {
ReportingApiServiceMock = {
getReport() {
return of(reportMockError['profitability-customer-JSON-error-1']);
}
};
} else {
ReportingApiServiceMock = {
getReport() {
return of(reportMock['profitability-customer']);
}
};
}
return ReportingApiServiceMock;
};