-
Notifications
You must be signed in to change notification settings - Fork 0
/
react-state-app.ts
139 lines (127 loc) · 5.57 KB
/
react-state-app.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
interface STATE {
id: number,
loading: true | false,
fetchedtodo: { id: number },
todo: null | { id: number }
}
interface CONTRACTSTATE {
isprev_step_setop: boolean
contractviolated: boolean
}
interface idobjinput { inputtype: string; value: number }
interface loadingobjinput { inputtype: string; value: true | false }
interface todoobjinput { inputtype: string; value: null | { id: number } }
type INPUT = idobjinput | loadingobjinput | todoobjinput | string;
const initialstate: STATE = {
id: 0,
loading: false,
fetchedtodo: { id: 0 },
todo: { id: 0 }
}
const initialcontractstate: CONTRACTSTATE = {
isprev_step_setop: false,
contractviolated: false
}
function transition(state: STATE, input: INPUT): STATE {
var newstate: STATE = { ...state };
var s = extractMatch(input)
switch (s) {
case "setid":
if (((typeof input === "object")) && (typeof input.value === "number")) {
newstate.id = input.value
}
break;
case "useref":
if (((typeof input === "object")) && (typeof input.value === "number")) {
newstate.id = input.value
}
break;
case "setloading":
if (((typeof input === "object")) && (typeof input.value === "boolean")) {
newstate.loading = input.value
}
break;
case "settodo":
newstate.todo = newstate.fetchedtodo
break;
case "fetchtodo":
state.fetchedtodo.id = newstate.id
break;
case "render":
break;
}
return newstate;
}
function transitioncontract(stateobj: STATE, contractobj: CONTRACTSTATE, input: INPUT) {
var newcontractstate: CONTRACTSTATE = { ...contractobj }
var nextinput = extractMatch(input)
switch (nextinput) {
case "setid":
newcontractstate.contractviolated = newcontractstate.isprev_step_setop ? true : false
newcontractstate.isprev_step_setop = true
break;
case "useref":
newcontractstate.contractviolated = newcontractstate.isprev_step_setop ? true : false
newcontractstate.isprev_step_setop = false
break;
case "setloading":
newcontractstate.contractviolated = newcontractstate.isprev_step_setop ? true : false
newcontractstate.isprev_step_setop = true
break;
case "settodo":
newcontractstate.contractviolated = newcontractstate.isprev_step_setop ? true : false
newcontractstate.isprev_step_setop = true
break;
case "fetchtodo":
newcontractstate.contractviolated = newcontractstate.isprev_step_setop ? true : false
newcontractstate.isprev_step_setop = false
break;
case "render":
var invalidrender = ((stateobj.todo !== null) && (stateobj.id != stateobj.todo.id) && (stateobj.loading == false))
newcontractstate.contractviolated = (invalidrender || !contractobj.isprev_step_setop)
newcontractstate.isprev_step_setop = false
break;
}
return newcontractstate;
}
function extractMatch(input: INPUT) {
return (typeof input == 'object') ? input.inputtype : input;
}
function runmultipleinputs(inputArray: Array<INPUT>): boolean {
var currentstate = initialstate;
var currentcontractstate = initialcontractstate
var output = false;
for (var x = 0; x < inputArray.length; x++) {
currentstate = transition(currentstate, inputArray[x])
currentcontractstate = transitioncontract(currentstate, currentcontractstate, inputArray[x])
if (currentcontractstate.contractviolated) {
output = true;
break;
}
}
return output
}
function checkunittest(result: boolean, expected: boolean) {
if (result == expected) {
return true
} else {
console.log("unit test failed")
return false;
}
}
var test1 = [{ inputtype: "setid", value: 2 }]
var test2 = [{ inputtype: "setid", value: 2 },"render"]
var test3 = [{ inputtype: "useref", value: 2 }, { inputtype: "setloading", value: true }, "fetchtodo", "settodo", { inputtype: "setloading", value: false }]
var test4 = [{ inputtype: "useref", value: 2 }, { inputtype: "setloading", value: true },"render", "fetchtodo", "settodo","render", { inputtype: "setloading", value: false },"render"]
var test5 = [{ inputtype: "useref", value: 2 },"render", { inputtype: "setloading", value: true },"render", "fetchtodo", "settodo","render", { inputtype: "setloading", value: false },"render"]
var test6 = [{ inputtype: "useref", value: 2 }, { inputtype: "setloading", value: true }, { inputtype: "setid", value: 3 }, "fetchtodo", "settodo", { inputtype: "setloading", value: false }]
var test7 = [{ inputtype: "useref", value: 2 }, { inputtype: "setloading", value: true },"render", { inputtype: "setid", value: 3 },"render", "fetchtodo", "settodo", "render",{ inputtype: "setloading", value: false },"render"]
var test8 = [{ inputtype: "useref", value: 2 }, { inputtype: "setloading", value: true }, "render",{ inputtype: "setid", value: 3 },"render", { inputtype: "setloading", value: false },"render", "fetchtodo", "settodo","render"]
checkunittest(runmultipleinputs(test1), false)
checkunittest(runmultipleinputs(test2), true)
checkunittest(runmultipleinputs(test3), true)
checkunittest(runmultipleinputs(test4), false)
checkunittest(runmultipleinputs(test5), true)
checkunittest(runmultipleinputs(test6), true)
checkunittest(runmultipleinputs(test7), false)
checkunittest(runmultipleinputs(test8), true)