This repository has been archived by the owner on Jun 17, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 154
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Test CreateNewProjectWizard components (#361)
* WIP: Added tests * WIP: ProjectType undefined not throwing in test * fixed flow * added tests * reverted to children render props * Merge branch 'master' into test-create-new-project-wiz * Fixed linting & replaced snapshot with simple smoke tests * addressed review comments
- Loading branch information
Showing
25 changed files
with
1,847 additions
and
65 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
92 changes: 92 additions & 0 deletions
92
src/components/CreateNewProjectWizard/__tests__/BuildPane.test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
import React from 'react'; | ||
import { shallow } from 'enzyme'; | ||
|
||
import BuildPane, { BUILD_STEP_KEYS } from '../BuildPane'; | ||
import createProject from '../../../services/create-project.service'; | ||
|
||
jest.mock('../../../services/create-project.service'); | ||
|
||
describe('BuildPane component', () => { | ||
let wrapper; | ||
let instance; | ||
const newProject = { | ||
projectName: 'New project', | ||
projectType: 'create-react-app', | ||
projectIcon: 'icon', | ||
projectStarter: '', | ||
}; | ||
|
||
const mockHandleCompleteBuild = jest.fn(); | ||
|
||
jest.useFakeTimers(); | ||
|
||
const shallowRenderBuildPane = project => | ||
shallow( | ||
<BuildPane | ||
{...project} | ||
status="filling-in-form" | ||
handleCompleteBuild={mockHandleCompleteBuild} | ||
/> | ||
); | ||
|
||
const testOutputs = ['Installing packages', 'Dependencies installed']; | ||
|
||
beforeEach(() => { | ||
wrapper = shallowRenderBuildPane(newProject); | ||
instance = wrapper.instance(); | ||
|
||
// Mock console errors so they don't output while running the test | ||
jest.spyOn(console, 'error'); | ||
console.error.mockImplementation(() => {}); | ||
}); | ||
|
||
afterEach(() => { | ||
console.error.mockRestore(); | ||
}); | ||
|
||
BUILD_STEP_KEYS.forEach(buildStep => { | ||
it(`should render build step ${buildStep}`, () => { | ||
wrapper.setProps({ | ||
currentBuildStep: buildStep, | ||
}); | ||
expect(wrapper).toMatchSnapshot(); | ||
}); | ||
}); | ||
|
||
it('should call createProject', () => { | ||
wrapper.setProps({ | ||
status: 'building-project', | ||
}); | ||
jest.runAllTimers(); | ||
expect(createProject).toHaveBeenCalled(); | ||
}); | ||
|
||
it('should throw error if a project prop is missing', () => { | ||
wrapper = shallowRenderBuildPane(); | ||
instance = wrapper.instance(); | ||
expect(instance.buildProject).toThrow(/insufficient data/i); | ||
}); | ||
|
||
it('should handle status updated', () => { | ||
// Note: Instance.handleStatusUpdate is called from createProject service | ||
// with data from stdout we're testing this here with a mock array of output strings | ||
testOutputs.forEach(output => { | ||
instance.handleStatusUpdate(output); | ||
expect(instance.state.currentBuildStep).toMatchSnapshot(); | ||
}); | ||
}); | ||
|
||
it('should call handleComplete', () => { | ||
// Note: HandleComplete is also called from createProject service | ||
// so it's OK to call this here directly as it's triggered | ||
// once the service finished the work. | ||
instance.handleComplete(newProject); | ||
jest.runAllTimers(); | ||
expect(mockHandleCompleteBuild).toHaveBeenCalledWith(newProject); | ||
}); | ||
|
||
it('should handle error message', () => { | ||
instance.handleError('npx: installed'); | ||
expect(instance.state.currentBuildStep).toEqual(BUILD_STEP_KEYS[1]); | ||
}); | ||
}); |
61 changes: 61 additions & 0 deletions
61
src/components/CreateNewProjectWizard/__tests__/BuildStepProgress.test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import React from 'react'; | ||
import { shallow } from 'enzyme'; | ||
|
||
import BuildStepProgress from '../BuildStepProgress'; | ||
|
||
describe('BuildStepProgress component', () => { | ||
let wrapper; | ||
let instance; | ||
|
||
const mockStep = { | ||
copy: 'Building...', | ||
additionalCopy: 'This may take some time', | ||
}; | ||
|
||
jest.useFakeTimers(); | ||
|
||
beforeEach(() => { | ||
wrapper = shallow(<BuildStepProgress step={mockStep} status="upcoming" />); | ||
instance = wrapper.instance(); | ||
}); | ||
|
||
it('should render upcoming', () => { | ||
expect(wrapper).toMatchSnapshot(); | ||
}); | ||
|
||
it('should render in-progress', () => { | ||
wrapper.setProps({ status: 'in-progress' }); | ||
expect(wrapper).toMatchSnapshot(); | ||
}); | ||
|
||
it('should render done', () => { | ||
wrapper.setProps({ status: 'done' }); | ||
expect(wrapper).toMatchSnapshot(); | ||
}); | ||
|
||
describe('Component logic', () => { | ||
it('should hide additional copy if done', () => { | ||
wrapper.setState({ | ||
shouldShowAdditionalCopy: true, | ||
}); | ||
wrapper.setProps({ | ||
status: 'done', | ||
}); | ||
expect(instance.state.shouldShowAdditionalCopy).toBeFalsy(); | ||
}); | ||
|
||
it('should show additonal copy after a delay', () => { | ||
wrapper.setProps({ | ||
status: 'in-progress', | ||
}); | ||
jest.runAllTimers(); | ||
expect(instance.state.shouldShowAdditionalCopy).toBeTruthy(); | ||
}); | ||
|
||
it('should clear timeout on unmount', () => { | ||
window.clearTimeout = jest.fn(); | ||
instance.componentWillUnmount(); | ||
expect(window.clearTimeout).toHaveBeenCalledWith(instance.timeoutId); | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.