Skip to content

Commit

Permalink
feat(mv3-part-3): Fix assessment store start over logic (#5435)
Browse files Browse the repository at this point in the history
  • Loading branch information
sfoslund committed Apr 27, 2022
1 parent 8ef4236 commit 0d6ff85
Show file tree
Hide file tree
Showing 3 changed files with 139 additions and 3 deletions.
6 changes: 4 additions & 2 deletions src/background/stores/assessment-store.ts
Expand Up @@ -413,14 +413,16 @@ export class AssessmentStore extends PersistentStore<AssessmentStoreData> {
private onResetData = (payload: ToggleActionPayload): void => {
const test = this.assessmentsProvider.forType(payload.test);
const config = test.getVisualizationConfiguration();
const defaultTestStatus: AssessmentData = config.getAssessmentData(this.getDefaultState());
const defaultTestStatus: AssessmentData = config.getAssessmentData(
this.generateDefaultState(null),
);
this.state.assessments[test.key] = defaultTestStatus;
this.state.assessmentNavState.selectedTestSubview = test.requirements[0].key;
this.emitChanged();
};

private onResetAllAssessmentsData = (targetTabId: number): void => {
this.state = this.getDefaultState();
this.state = this.generateDefaultState(null);
this.updateTargetTabWithId(targetTabId);
};

Expand Down
Expand Up @@ -2,4 +2,6 @@

exports[`AssessmentStore onResetAllAssessmentsData 1`] = `"tab with Id 1000 not found"`;

exports[`AssessmentStore onResetAllAssessmentsData with persisted data 1`] = `"tab with Id 1000 not found"`;

exports[`AssessmentStore onUpdateTargetTabId 1`] = `"tab with Id 1000 not found"`;
134 changes: 133 additions & 1 deletion src/tests/unit/tests/background/stores/assessment-store.test.ts
Expand Up @@ -295,6 +295,62 @@ describe('AssessmentStore', () => {
.testListenerToBeCalledOnce(initialState, expectedState);
});

test('on resetData: only reset data for one test with persisted data', () => {
const expectedInstanceMap = {};
const expectedManualTestStepResultMap = {};

const assessmentData = new AssessmentDataBuilder()
.with('generatedAssessmentInstancesMap', expectedInstanceMap)
.with('manualTestStepResultMap', expectedManualTestStepResultMap)
.with('testStepStatus', getSampleTestStepsData())
.build();

const initialState = getStateWithAssessment(assessmentData);
const expectedState = getDefaultStateWithDefaultAssessmentData(
assessmentKey,
requirementKey,
);
const getVisualizationConfigurationMock = Mock.ofInstance(() => {});
const visualizationConfigStub = {
getAssessmentData: state => {
return state.assessments[assessmentKey];
},
} as AssessmentVisualizationConfiguration;

getVisualizationConfigurationMock
.setup(configGetter => configGetter())
.returns(() => {
return visualizationConfigStub;
});

const assessmentStub = {
getVisualizationConfiguration: getVisualizationConfigurationMock.object,
key: assessmentKey,
requirements: [
{
key: requirementKey,
},
],
} as Assessment;

assessmentsProviderMock
.setup(apm => apm.forType(assessmentType))
.returns(() => assessmentStub);

// Called with persisted data from initialize
setupDataGeneratorMock(initialState, getDefaultState(), Times.once());
// Called without persisted data from resetData
setupDataGeneratorMock(null, expectedState, Times.once());

const payload: ToggleActionPayload = {
test: assessmentType,
};

createStoreTesterForAssessmentActions('resetData', initialState)
.withActionParam(payload)
.testListenerToBeCalledOnce(initialState, expectedState);
});

test('test that tests indexedDB and also reset', () => {
const expectedInstanceMap = {};
const expectedManualTestStepResultMap = {};
Expand Down Expand Up @@ -442,6 +498,81 @@ describe('AssessmentStore', () => {
expect(() => rejectCb()).toThrowErrorMatchingSnapshot();
});

test('onResetAllAssessmentsData with persisted data', () => {
const persisted: AssessmentStoreData = {
persistedTabInfo: null,
assessments: {
['assessment-1']: {
fullAxeResultsMap: null,
generatedAssessmentInstancesMap: null,
manualTestStepResultMap: {
['assessment-1-step-1']: {
instances: [],
status: 2,
id: 'assessment-1-step-1',
},
['removed-step']: {
instances: [],
status: 2,
id: '123',
},
},
testStepStatus: {},
},
},
assessmentNavState: {
selectedTestType: null,
selectedTestSubview: null,
},
resultDescription: '',
};

const oldTabId = 1;
const tabId = 1000;
const url = 'url';
const title = 'title';
const tab: Tab = {
id: tabId,
url,
title,
};
let rejectCb;
browserMock
.setup(b => b.getTab(tabId, It.isAny(), It.isAny()))
.returns((id, resolve, reject) => {
rejectCb = reject;
resolve(tab);
})
.verifiable();

assessmentsProviderMock.setup(apm => apm.all()).returns(() => assessmentsProvider.all());
const initialState = new AssessmentsStoreDataBuilder(
assessmentsProvider,
assessmentDataConverterMock.object,
)
.withSelectedTestType(VisualizationType.Color)
.withTargetTab(oldTabId, null, null, true)
.build();

const finalState = new AssessmentsStoreDataBuilder(
assessmentsProvider,
assessmentDataConverterMock.object,
)
.withTargetTab(tabId, url, title, false)
.build();

// Called with persisted data from initialize
setupDataGeneratorMock(persisted, getDefaultState(), Times.once());
// Called without persisted data from resetAllAssessmentsData
setupDataGeneratorMock(null, getDefaultState(), Times.once());

createStoreTesterForAssessmentActions('resetAllAssessmentsData', persisted)
.withActionParam(tabId)
.testListenerToBeCalledOnce(initialState, finalState);

expect(() => rejectCb()).toThrowErrorMatchingSnapshot();
});

test('onContinuePreviousAssessment', () => {
const oldTabId = 1;
const tabId = 1000;
Expand Down Expand Up @@ -2019,6 +2150,7 @@ describe('AssessmentStore', () => {

function createStoreTesterForAssessmentActions(
actionName: keyof AssessmentActions,
persistedData: AssessmentStoreData = null,
): AssessmentStoreTester<AssessmentStoreData, AssessmentActions> {
const factory = (actions: AssessmentActions) =>
new AssessmentStore(
Expand All @@ -2028,7 +2160,7 @@ describe('AssessmentStore', () => {
assessmentDataRemoverMock.object,
assessmentsProviderMock.object,
indexDBInstanceMock.object,
null,
persistedData,
initialAssessmentStoreDataGeneratorMock.object,
failTestOnErrorLogger,
);
Expand Down

0 comments on commit 0d6ff85

Please sign in to comment.