Skip to content

Instantly share code, notes, and snippets.

@apark0720
Last active June 17, 2019 02:36
Show Gist options
  • Save apark0720/28d0a5c07d39b45604ae5e3bfddf5ca0 to your computer and use it in GitHub Desktop.
Save apark0720/28d0a5c07d39b45604ae5e3bfddf5ca0 to your computer and use it in GitHub Desktop.
export const actionTypes = {
ADD_ASSERTION: 'ADD_ASSERTION',
DELETE_ASSERTION: 'DELETE_ASSERTION',
};
export const addAction = () => ({
type: actionTypes.ADD_ACTION,
});
export const deleteAction = id => ({
type: actionTypes.DELETE_ACTION,
id,
});
import { createContext } from 'react';
import { actionTypes } from './testCaseActions';
export const TestCaseContext = createContext(null);
export const testCaseState = {
testStatement: '',
statements: [
{
id: 0,
type: 'render',
componentName: '',
filePath: '',
props: [],
hasProp: false,
},
{
id: 1,
type: 'assertion',
queryVariant: '',
querySelector: '',
queryValue: '',
isNot: false,
matcherType: '',
matcherValue: '',
suggestions: [],
},
],
};
let statementId = 2;
const createAssertion = () => ({
id: statementId++,
type: 'assertion',
queryVariant: '',
querySelector: '',
queryValue: '',
isNot: false,
matcherType: '',
matcherValue: '',
suggestions: [],
});
export const testCaseReducer = (state, action) => {
Object.freeze(state);
let statements = [...state.statements];
let lastAssertionStatement;
switch (action.type) {
case actionTypes.ADD_ASSERTION:
lastAssertionStatement = statements.pop();
statements.push(createAssertion(), lastAssertionStatement);
return {
...state,
statements,
};
case actionTypes.DELETE_ASSERTION:
lastAssertionStatement = statements.pop();
statements = statements.filter(statement => statement.id !== action.id);
statements.push(lastAssertionStatement);
return {
...state,
statements,
};
default:
return state;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment