This commit is contained in:
Nathan Bierema 2020-09-12 20:03:18 -04:00
parent 1d6d3867d2
commit 407a5bac43
5 changed files with 35 additions and 18 deletions

View File

@ -13,18 +13,21 @@ import { AssertionLocals, DispatcherLocals, WrapLocals } from './types';
export const fromPath = (path: (string | number)[]) => export const fromPath = (path: (string | number)[]) =>
path.map((a) => (typeof a === 'string' ? `.${a}` : `[${a}]`)).join(''); path.map((a) => (typeof a === 'string' ? `.${a}` : `[${a}]`)).join('');
// eslint-disable-next-line @typescript-eslint/ban-types function getState<S>(
function getState<S>(s: { state: S; error?: string }, defaultValue: {}) { s: { state: S; error?: string } | undefined,
// eslint-disable-next-line @typescript-eslint/ban-types
defaultValue?: {}
) {
if (!s) return defaultValue; if (!s) return defaultValue;
return JSON.parse(jsan.stringify(s.state)); return JSON.parse(jsan.stringify(s.state));
} }
export function compare<S>( export function compare<S>(
s1: { state: S; error?: string }, s1: { state: S; error?: string } | undefined,
s2: { state: S; error?: string }, s2: { state: S; error?: string },
cb: (value: { path: string; curState: number | string | undefined }) => void, cb: (value: { path: string; curState: number | string | undefined }) => void,
// eslint-disable-next-line @typescript-eslint/ban-types // eslint-disable-next-line @typescript-eslint/ban-types
defaultValue: {} defaultValue?: {}
) { ) {
const paths: string[] = []; // Already processed const paths: string[] = []; // Already processed
function generate( function generate(

View File

@ -1,28 +1,42 @@
import React from 'react'; import React from 'react';
import { render } from 'enzyme'; import { render } from 'enzyme';
import { renderToJson } from 'enzyme-to-json'; import { renderToJson } from 'enzyme-to-json';
import { PerformAction } from 'redux-devtools-instrument';
import { Action } from 'redux';
import TestGenerator from '../src/TestGenerator'; import TestGenerator from '../src/TestGenerator';
import fnTemplate from '../src/redux/mocha'; import fnTemplate from '../src/redux/mocha';
import strTemplate from '../src/redux/mocha/template'; import strTemplate from '../src/redux/mocha/template';
import fnVanillaTemplate from '../src/vanilla/mocha'; import fnVanillaTemplate from '../src/vanilla/mocha';
import strVanillaTemplate from '../src/vanilla/mocha/template'; import strVanillaTemplate from '../src/vanilla/mocha/template';
const actions = { const actions: { [actionId: number]: PerformAction<Action<unknown>> } = {
0: { type: 'PERFORM_ACTION', action: { type: '@@INIT' } }, 0: {
1: { type: 'PERFORM_ACTION', action: { type: 'INCREMENT_COUNTER' } }, type: 'PERFORM_ACTION',
action: { type: '@@INIT' },
timestamp: 0,
stack: undefined,
},
1: {
type: 'PERFORM_ACTION',
action: { type: 'INCREMENT_COUNTER' },
timestamp: 0,
stack: undefined,
},
}; };
const computedStates = [{ state: { counter: 0 } }, { state: { counter: 1 } }]; const computedStates = [{ state: { counter: 0 } }, { state: { counter: 1 } }];
const TestGeneratorAsAny = TestGenerator as any;
describe('TestGenerator component', () => { describe('TestGenerator component', () => {
it('should show warning message when no params provided', () => { it('should show warning message when no params provided', () => {
const component = render(<TestGenerator useCodemirror={false} />); const component = render(<TestGeneratorAsAny useCodemirror={false} />);
expect(renderToJson(component)).toMatchSnapshot(); expect(renderToJson(component)).toMatchSnapshot();
}); });
it('should be empty when no actions provided', () => { it('should be empty when no actions provided', () => {
const component = render( const component = render(
<TestGenerator <TestGeneratorAsAny
assertion={fnTemplate.assertion} assertion={fnTemplate.assertion}
dispatcher={fnTemplate.dispatcher} dispatcher={fnTemplate.dispatcher}
wrap={fnTemplate.wrap} wrap={fnTemplate.wrap}
@ -34,7 +48,7 @@ describe('TestGenerator component', () => {
it("should match function template's test for first action", () => { it("should match function template's test for first action", () => {
const component = render( const component = render(
<TestGenerator <TestGeneratorAsAny
assertion={fnTemplate.assertion} assertion={fnTemplate.assertion}
dispatcher={fnTemplate.dispatcher} dispatcher={fnTemplate.dispatcher}
wrap={fnTemplate.wrap} wrap={fnTemplate.wrap}
@ -49,7 +63,7 @@ describe('TestGenerator component', () => {
it("should match string template's test for first action", () => { it("should match string template's test for first action", () => {
const component = render( const component = render(
<TestGenerator <TestGeneratorAsAny
assertion={strTemplate.assertion} assertion={strTemplate.assertion}
dispatcher={strTemplate.dispatcher} dispatcher={strTemplate.dispatcher}
wrap={strTemplate.wrap} wrap={strTemplate.wrap}
@ -64,7 +78,7 @@ describe('TestGenerator component', () => {
it('should generate test for the last action when selectedActionId not specified', () => { it('should generate test for the last action when selectedActionId not specified', () => {
const component = render( const component = render(
<TestGenerator <TestGeneratorAsAny
assertion={fnTemplate.assertion} assertion={fnTemplate.assertion}
dispatcher={fnTemplate.dispatcher} dispatcher={fnTemplate.dispatcher}
wrap={fnTemplate.wrap} wrap={fnTemplate.wrap}
@ -78,7 +92,7 @@ describe('TestGenerator component', () => {
it('should generate test for vanilla js class', () => { it('should generate test for vanilla js class', () => {
const component = render( const component = render(
<TestGenerator <TestGeneratorAsAny
assertion={fnVanillaTemplate.assertion} assertion={fnVanillaTemplate.assertion}
dispatcher={fnVanillaTemplate.dispatcher} dispatcher={fnVanillaTemplate.dispatcher}
wrap={fnVanillaTemplate.wrap} wrap={fnVanillaTemplate.wrap}
@ -95,7 +109,7 @@ describe('TestGenerator component', () => {
it('should generate test for vanilla js class with string template', () => { it('should generate test for vanilla js class with string template', () => {
const component = render( const component = render(
<TestGenerator <TestGeneratorAsAny
assertion={strVanillaTemplate.assertion} assertion={strVanillaTemplate.assertion}
dispatcher={strVanillaTemplate.dispatcher} dispatcher={strVanillaTemplate.dispatcher}
wrap={strVanillaTemplate.wrap} wrap={strVanillaTemplate.wrap}

View File

@ -12,11 +12,11 @@ const computedStates = [
{ state: [0, 2, 3, 4] }, { state: [0, 2, 3, 4] },
]; ];
const test = (s1, s2) => const test = (s1: { state: unknown } | undefined, s2: { state: unknown }) =>
compare(s1, s2, ({ path, curState }) => compare(s1, s2, ({ path, curState }) =>
expect(`expect(store${path}).toEqual(${curState});`).toBe( expect(
assertion({ path, curState }) `expect(store${path}).toEqual(${curState as number | string});`
) ).toBe(assertion({ path, curState }))
); );
describe('Assertions', () => { describe('Assertions', () => {