Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move to ts and jest #6

Merged
merged 3 commits into from
Mar 4, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"presets": [
"@babel/preset-env",
"@babel/preset-react",
"@babel/preset-typescript"
]
}
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@
/lib
/node_modules
/umd
/build
npm-debug.log*
48 changes: 48 additions & 0 deletions __tests__/Wizard.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import React from 'react';
import {render, fireEvent, cleanup, waitForElement} from 'react-testing-library'
// adds special assertions like toHaveTextContent
import 'jest-dom/extend-expect';

import { Wizard } from '../src/';

describe('Wizard Component', () => {
afterEach(cleanup);

test('displays a welcome message', () => {
const { getByText } = render(
<Wizard initialValues={{}} onSubmit={null}>
<Wizard.Step>{() => <h2>@rahsheen/React-Wizard</h2>}</Wizard.Step>
</Wizard>
);

expect(getByText('@rahsheen/React-Wizard'))
});

test("does not render without children", () => {
// @ts-ignore
const {container} = render(<Wizard initialValues={{}} onSubmit={null}></Wizard>)
expect(container.childElementCount).toBe(0)
})

test("doesn't render disabled steps", () => {
const {container, getByText} = render(
<Wizard initialValues={{}} onSubmit={null}>
<Wizard.Step>{() => <h2>rahsheen/React-Wizard</h2>}</Wizard.Step>
<Wizard.Step disabled={true}>{() => <h2>Step 2</h2>}</Wizard.Step>
</Wizard>
)

expect(container.childElementCount).toBe(1)
})

test("does not render future steps", () => {
const {container} = render(
<Wizard initialValues={{}} onSubmit={null}>
<Wizard.Step>{() => <h2>rahsheen/React-Wizard</h2>}</Wizard.Step>
<Wizard.Step>{() => <h2>Step 2</h2>}</Wizard.Step>
</Wizard>
)

expect(container.childElementCount).toBe(1)
})
})
101 changes: 101 additions & 0 deletions __tests__/useWizard.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
import React from 'react';
import { render, fireEvent, cleanup, waitForElement, act } from 'react-testing-library';
// adds special assertions like toHaveTextContent
import 'jest-dom/extend-expect';

import { useWizard } from '../src/';

describe('Wizard Hook', () => {
const Wizard = ({ children, ...rest }) => children(useWizard(rest));

function setup(props = {}) {
const returnVal = {};
render(
<Wizard {...props}>
{(val: any) => {
Object.assign(returnVal, val);
return null;
}}
</Wizard>
);
return returnVal;
}

afterEach(cleanup);

it('moves to the next step', () => {
const wizardData = setup();
expect(wizardData.index).toBe(0);
act(() => wizardData.nextStep());
expect(wizardData.index).toBe(1);
});

it('moves to the previous step', () => {
const wizardData = setup({ size: 5 });
expect(wizardData.index).toBe(0);
wizardData.nextStep();
wizardData.nextStep();
expect(wizardData.index).toBe(2);
wizardData.prevStep();
expect(wizardData.index).toBe(1);
});

it('Does not decrease index below 0', () => {
const wizardData = setup();
expect(wizardData.index).toBe(0);
wizardData.prevStep();
expect(wizardData.index).toBe(0);
});

it('Does not increase index above size', () => {
const wizardData = setup({ size: 3 });
expect(wizardData.index).toBe(0);
wizardData.nextStep();
wizardData.nextStep();
wizardData.nextStep();
wizardData.nextStep();
wizardData.nextStep();
// Zero-based index means last index is 2
expect(wizardData.index).toBe(2);
});

it('sets initial values', () => {
const initialValues = {
foo: 'Bippety',
bar: 3,
baz: () => {}
};

const wizardData = setup({ initialValues });
expect(JSON.stringify(wizardData.values)).toBe(JSON.stringify(initialValues));
});

it('changes values', () => {
const initialValues = {
foo: 'Bippety',
bar: 3,
baz: () => {}
};

const wizardData = setup({ initialValues });
wizardData.onChangeValue('bar', 100);
expect(JSON.stringify(wizardData.values)).toBe(JSON.stringify({ ...initialValues, bar: 100 }));
});

it('submits values', () => {
const initialValues = {
foo: 'Bippety',
bar: 3,
baz: () => {}
};

const onSubmit = args => {
expect(JSON.stringify(wizardData.values)).toBe(JSON.stringify(args));
}

const wizardData = setup({ initialValues, onSubmit });

wizardData.onSubmit();

})
});
11 changes: 10 additions & 1 deletion demo/src/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React, { Component } from "react"
import { render } from "react-dom"
import { Wizard } from "../../src/"
import { Wizard } from "../../build/lib/"

class Demo extends Component {
render() {
Expand Down Expand Up @@ -28,6 +28,15 @@ class Demo extends Component {
</div>
)}
</Wizard.Step>
<Wizard.Step disabled>
{({ nextStep, prevStep, currentIndex }) => (
<div>
<h2>Bip {currentIndex}</h2>
<button onClick={prevStep}>Back</button>
<button onClick={nextStep}>Next</button>
</div>
)}
</Wizard.Step>
<Wizard.Step>
{({ nextStep, prevStep, currentIndex }) => (
<div>
Expand Down
Loading