-
Notifications
You must be signed in to change notification settings - Fork 54
/
Tests.js
111 lines (92 loc) · 1.75 KB
/
Tests.js
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
import { registerStore } from '@wordpress/data';
import apiFetch from "@wordpress/api-fetch";
apiFetch.use( apiFetch.createNonceMiddleware( SiteHealth.nonce.api_nonce ) );
const SiteHealth_DefaultState_Tests = {
tests: [],
isLoading: true,
results: {
good: [],
recommended: [],
critical: [],
},
};
const actions = {
setLoading( loading ) {
return {
type: 'SET_LOADING',
loading
};
},
setTests( tests ) {
return {
type: 'SET_TESTS',
tests,
};
},
setTestResult( test, result ) {
return {
type: 'SET_TEST_RESULT',
test,
result,
};
},
fetchFromAPI( path ) {
return {
type: 'FETCH_FROM_API',
path,
};
},
};
registerStore(
'site-health-tests', {
reducer( state = SiteHealth_DefaultState_Tests, action ) {
switch ( action.type ) {
case 'SET_LOADING':
return {
...state,
isLoading: action.loading
};
case 'SET_TESTS':
return {
...state,
tests: action.tests
};
case 'SET_TEST_RESULT':
let results = state.results;
results[ action.result ][ action.test.name ] = action.test;
return {
...state,
results: currentResults
};
}
return state;
},
actions,
selectors: {
getTest( state, item ) {
return state.tests[ item ];
},
getResults( state, type ) {
return state.results[ type ];
},
getTests( state ) {
return state.tests;
},
isLoading( state ) {
return state.isLoading;
}
},
controls: {
FETCH_FROM_API( action ) {
return apiFetch( { path: action.path } );
}
},
resolvers: {
* getTests() {
const path = '/wp-json/health-check/site-health/v1/get-tests';
const tests = yield actions.fetchFromAPI( path );
return actions.setTests( tests );
}
}
}
);