forked from microsoft/accessibility-insights-web
-
Notifications
You must be signed in to change notification settings - Fork 0
/
create-initial-assessment-test-data.ts
162 lines (146 loc) · 5.21 KB
/
create-initial-assessment-test-data.ts
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
import { Assessment } from 'assessments/types/iassessment';
import {
ManualTestStatus,
ManualTestStatusData,
TestStepData,
} from 'common/types/store-data/manual-test-status';
import { isEmpty, pick } from 'lodash';
import {
AssessmentData,
GeneratedAssessmentInstance,
InstanceIdToInstanceDataMap,
ManualTestStepResult,
RequirementIdToResultMap,
} from '../common/types/store-data/assessment-result-data';
import { DictionaryStringTo } from '../types/common-types';
export type InitialDataCreator = (
test: Readonly<Assessment>,
persistedTest: AssessmentData,
) => AssessmentData;
export const createInitialAssessmentTestData: InitialDataCreator = (
test: Readonly<Assessment>,
persistedTest: AssessmentData,
) => {
const requirements = test.requirements.map(val => val.key);
if (persistedTest) {
return getInitialTestDataUsingPersistedData(
requirements,
persistedTest.testStepStatus,
persistedTest.manualTestStepResultMap,
persistedTest.generatedAssessmentInstancesMap,
);
}
return getDefaultInitialTestData(requirements);
};
export const createAutomatedChecksInitialAssessmentTestData: InitialDataCreator = (
test: Readonly<Assessment>,
persistedTest: AssessmentData,
) => {
const requirements = test.requirements.map(val => val.key);
if (persistedTest && allRequirementsAreScanned(requirements, persistedTest)) {
return getInitialTestDataUsingPersistedData(
requirements,
persistedTest.testStepStatus,
persistedTest.manualTestStepResultMap,
persistedTest.generatedAssessmentInstancesMap,
);
}
return getDefaultInitialTestData(requirements);
};
function getDefaultInitialTestData(requirements: string[]): AssessmentData {
return getInitialTestDataUsingPersistedData(requirements, {}, {}, undefined);
}
function getInitialTestDataUsingPersistedData(
requirements: string[],
persistedRequirementsStatus: ManualTestStatusData,
persistedManualMap?: RequirementIdToResultMap,
persistedGeneratedMap?: InstanceIdToInstanceDataMap,
): AssessmentData {
const testData: AssessmentData = getDefaultTestResult();
testData.testStepStatus = constructRequirementStatus(requirements, persistedRequirementsStatus);
testData.manualTestStepResultMap = constructManualRequirementResultMap(
requirements,
persistedManualMap,
);
testData.generatedAssessmentInstancesMap = constructGeneratedAssessmentInstancesMap(
requirements,
persistedGeneratedMap,
);
return testData;
}
function allRequirementsAreScanned(requirements: string[], persistedTest: AssessmentData): boolean {
return requirements.every(
requirement =>
persistedTest.testStepStatus[requirement] &&
persistedTest.testStepStatus[requirement].isStepScanned === true,
);
}
function getDefaultTestResult(): AssessmentData {
return {
fullAxeResultsMap: null,
generatedAssessmentInstancesMap: undefined,
manualTestStepResultMap: {},
testStepStatus: {},
};
}
function getDefaultRequirementStatus(): TestStepData {
return { stepFinalResult: ManualTestStatus.UNKNOWN, isStepScanned: false };
}
function getDefaultManualRequirementResult(step: string): ManualTestStepResult {
return { status: ManualTestStatus.UNKNOWN, id: step, instances: [] };
}
function constructRequirementStatus(
requirements: string[],
persistedMap: ManualTestStatusData,
): ManualTestStatusData {
return constructMapFromRequirementTo<TestStepData>(
requirements,
persistedMap,
getDefaultRequirementStatus,
);
}
function constructManualRequirementResultMap(
requirements: string[],
persistedMap?: RequirementIdToResultMap,
): RequirementIdToResultMap {
return constructMapFromRequirementTo<ManualTestStepResult>(
requirements,
persistedMap,
getDefaultManualRequirementResult,
);
}
function constructMapFromRequirementTo<T>(
requirements: string[],
persistedMap: DictionaryStringTo<T> | undefined,
getDefaultData: (req: string) => T,
): DictionaryStringTo<T> {
const map: DictionaryStringTo<T> = {};
requirements.forEach(requirement => {
map[requirement] =
(persistedMap && persistedMap[requirement]) || getDefaultData(requirement);
});
return map;
}
function constructGeneratedAssessmentInstancesMap(
requirements: string[],
persistedMap?: InstanceIdToInstanceDataMap,
): InstanceIdToInstanceDataMap | undefined {
const map: InstanceIdToInstanceDataMap = {};
if (isEmpty(persistedMap)) {
return undefined;
}
Object.keys(persistedMap).forEach(instanceId => {
const instanceData: GeneratedAssessmentInstance = persistedMap[instanceId];
const filteredResultMap = pick(instanceData.testStepResults, requirements);
if (!isEmpty(filteredResultMap)) {
instanceData.testStepResults = filteredResultMap;
map[instanceId] = instanceData;
}
});
if (isEmpty(map)) {
return undefined;
}
return map;
}