| |
| new file mode 100644 |
| |
| |
| |
| @@ -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` |
| + ); |
| + }); |
| +}); |
| |
| new file mode 100755 |
| |
| |
| @@ -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 |
|
|