File size: 6,880 Bytes
f4a5c87
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
diff --git a/test/create-market/selectors/form-steps/step-2-categorical-validation-test.js b/test/create-market/selectors/form-steps/step-2-categorical-validation-test.js
new file mode 100644
index 000000000..b70aabca2
--- /dev/null
+++ b/test/create-market/selectors/form-steps/step-2-categorical-validation-test.js
@@ -0,0 +1,166 @@
+import { assert } from 'chai';
+import { CATEGORICAL } from '../../../../src/modules/markets/constants/market-types';
+import * as selector from '../../../../src/modules/create-market/selectors/form-steps/step-2';
+
+const validDescription = 'This is a valid categorical market description for validation tests.';
+
+const makeValidCategoricalFormState = (overrides = {}) => {
+	const formState = {
+		type: CATEGORICAL,
+		description: validDescription,
+		endDate: Date.now() + (7 * 24 * 60 * 60 * 1000),
+		categoricalOutcomes: ['Alpha', 'Beta', 'Gamma']
+	};
+	return Object.assign({}, formState, overrides);
+};
+
+const hasNoCategoricalOutcomeErrors = (errors) => {
+	if (!Object.prototype.hasOwnProperty.call(errors, 'categoricalOutcomes')) return true;
+	return Array.isArray(errors.categoricalOutcomes) && errors.categoricalOutcomes.length === 0;
+};
+
+const assertOutcomeErrors = (actual, expectedLength, expectedMessagesByIndex) => {
+	assert.isArray(actual, `categorical outcome errors should be returned as a position-preserving array`);
+	assert.strictEqual(
+		actual.length,
+		expectedLength,
+		`categorical outcome error output should preserve submitted outcome positions`
+	);
+
+	const expectedMessages = expectedMessagesByIndex.reduce((memo, item) => {
+		memo[item.index] = item.message;
+		return memo;
+	}, {});
+
+	for (let index = 0; index < expectedLength; index++) {
+		if (Object.prototype.hasOwnProperty.call(expectedMessages, index)) {
+			assert.strictEqual(
+				actual[index],
+				expectedMessages[index],
+				`expected categorical outcome error at submitted position ${index}`
+			);
+		} else {
+			assert.notOk(actual[index], `submitted position ${index} should not report a categorical outcome error`);
+		}
+	}
+};
+
+describe(`modules/create-market/selectors/form-steps/step-2 categorical outcome validation`, () => {
+	it(`reports each duplicated outcome by position`, () => {
+		let errs = selector.errors(makeValidCategoricalFormState({
+			categoricalOutcomes: ['Alpha', 'Beta', 'Alpha', 'Gamma']
+		}));
+
+		assertOutcomeErrors(
+			errs.categoricalOutcomes,
+			4,
+			[
+				{ index: 0, message: 'Category must be unique' },
+				{ index: 2, message: 'Category must be unique' }
+			]
+		);
+
+		errs = selector.errors(makeValidCategoricalFormState({
+			categoricalOutcomes: ['One', 'Two', 'Three', 'Two']
+		}));
+
+		assertOutcomeErrors(
+			errs.categoricalOutcomes,
+			4,
+			[
+				{ index: 1, message: 'Category must be unique' },
+				{ index: 3, message: 'Category must be unique' }
+			]
+		);
+	});
+
+	it(`reports blank outcomes by position`, () => {
+		let errs = selector.errors(makeValidCategoricalFormState({
+			categoricalOutcomes: ['Yes', '', 'No', '']
+		}));
+
+		assertOutcomeErrors(
+			errs.categoricalOutcomes,
+			4,
+			[
+				{ index: 1, message: 'Answer cannot be blank' },
+				{ index: 3, message: 'Answer cannot be blank' }
+			]
+		);
+
+		errs = selector.errors(makeValidCategoricalFormState({
+			categoricalOutcomes: ['Up', 'Down', '', 'Sideways']
+		}));
+
+		assertOutcomeErrors(
+			errs.categoricalOutcomes,
+			4,
+			[
+				{ index: 2, message: 'Answer cannot be blank' }
+			]
+		);
+	});
+
+	it(`reports blank outcomes when every submitted value is blank`, () => {
+		const errs = selector.errors(makeValidCategoricalFormState({
+			categoricalOutcomes: ['', '', '']
+		}));
+
+		assert.property(errs, 'categoricalOutcomes', `categorical outcome errors should be populated when categorical outcomes are present`);
+		assert.deepEqual(
+			errs.categoricalOutcomes,
+			['Answer cannot be blank', 'Answer cannot be blank', 'Answer cannot be blank'],
+			`each submitted blank categorical outcome should receive a blank-answer error`
+		);
+	});
+
+	it(`treats empty categorical outcomes as no categorical outcome errors`, () => {
+		const errs = selector.errors(makeValidCategoricalFormState({
+			categoricalOutcomes: []
+		}));
+
+		assert.isTrue(
+			hasNoCategoricalOutcomeErrors(errs),
+			`an empty categorical-outcomes input should not create categorical outcome errors`
+		);
+		assert.strictEqual(
+			selector.isValid(makeValidCategoricalFormState({ categoricalOutcomes: [] })),
+			true,
+			`empty categorical outcomes should not make an otherwise valid categorical step invalid`
+		);
+	});
+
+	it(`treats missing categorical outcomes as no categorical outcome errors`, () => {
+		const formState = makeValidCategoricalFormState();
+		delete formState.categoricalOutcomes;
+
+		assert.doesNotThrow(() => selector.errors(formState), `missing categoricalOutcomes should not throw`);
+
+		const errs = selector.errors(formState);
+		assert.isTrue(
+			hasNoCategoricalOutcomeErrors(errs),
+			`missing categoricalOutcomes should not create categorical outcome errors`
+		);
+		assert.strictEqual(
+			selector.isValid(formState),
+			true,
+			`missing categoricalOutcomes should not make an otherwise valid categorical step invalid`
+		);
+	});
+
+	it(`isValid rejects duplicate outcomes`, () => {
+		assert.strictEqual(
+			selector.isValid(makeValidCategoricalFormState({ categoricalOutcomes: ['Red', 'Green', 'Red'] })),
+			false,
+			`duplicate categorical outcomes should make the categorical step invalid`
+		);
+	});
+
+	it(`isValid rejects blank outcomes`, () => {
+		assert.strictEqual(
+			selector.isValid(makeValidCategoricalFormState({ categoricalOutcomes: ['Red', '', 'Blue'] })),
+			false,
+			`blank categorical outcomes should make the categorical step invalid`
+		);
+	});
+});
diff --git a/mimo_test_command.sh b/mimo_test_command.sh
new file mode 100755
--- /dev/null
+++ b/mimo_test_command.sh
@@ -0,0 +1,34 @@
+#!/bin/bash
+set -uo pipefail
+cd /testbed
+
+
+
+npm install --ignore-scripts 2>/dev/null
+
+mkdir -p /testbed/.fixture_tmp
+cat > /testbed/.fixture_tmp/date-now-shim.js <<'SHIMEOF'
+var _origNow = Date.now.bind(Date);
+Date.now = function DateNowProxy() { return _origNow(); };
+SHIMEOF
+
+
+case "${1:-}" in
+  base)
+    NODE_ENV=test node_modules/.bin/mocha \
+      --compilers js:babel-register \
+      --require /testbed/.fixture_tmp/date-now-shim.js \
+      test/create-market/selectors/form-steps/step-3-test.js
+    ;;
+  new)
+    NODE_ENV=test node_modules/.bin/mocha \
+      --compilers js:babel-register \
+      --require /testbed/.fixture_tmp/date-now-shim.js \
+      test/create-market/selectors/form-steps/step-2-categorical-validation-test.js \
+      test/create-market/selectors/form-steps/step-3-test.js
+    ;;
+  *)
+    echo "usage: $0 base|new"
+    exit 2
+    ;;
+esac