Skip to content
This repository has been archived by the owner on Mar 31, 2022. It is now read-only.

Commit

Permalink
Write first typescript react test with enzyme
Browse files Browse the repository at this point in the history
Note that the 'test:watch' script is not currently working.  It runs the
tests fine, but will not run again.  I think this may have happened when
I switched to ts-mocha, because it was working before with just mocha.

However, I think that this thread might hold the answer:
  TypeStrong/ts-node#266
  • Loading branch information
0livare committed Mar 12, 2018
1 parent 9aebc20 commit 4b34083
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 6 deletions.
6 changes: 6 additions & 0 deletions bld/testSetup.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,9 @@ global.navigator = {
};

documentRef = document; //eslint-disable-line no-undef


// Configure the adapter required by enzyme
var enzyme = require('enzyme');
var Adapter = require('enzyme-adapter-react-16');
enzyme.configure({ adapter: new Adapter() });
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
"localtunnel": "lt --port 3000 --subdomain posten",
"share": "npm-run-all --parallel open:src localtunnel",
"ztest": "ts-mocha --reporter spec bld/testSetup.js \"src/**/*.test.tsx\"",
"test": "ts-mocha --reporter progress bld/testSetup.js src/**/*.test.tsx src/components/**/*.test.tsx",
"test:watch": "npm run test -- --watch",
"test": "ts-mocha --reporter spec bld/testSetup.js src/**/*.test.tsx",
"test:watch": "ts-mocha --watch --reporter progress bld/testSetup.js src/**/*.test.tsx",
"generate-mock-data": "babel-node bld/generateMockData",
"prestart-mockapi": "npm run generate-mock-data",
"start-mockapi": "json-server --watch src/api/db.json --port 3001 --delay 1000",
Expand Down Expand Up @@ -85,6 +85,7 @@
"@types/mocha": "^2.2.48",
"chai": "^4.1.2",
"enzyme": "^3.3.0",
"enzyme-adapter-react-16": "^1.1.1",
"eslint": "3.8.1",
"eslint-plugin-import": "2.0.1",
"eslint-plugin-react": "^7.7.0",
Expand Down
7 changes: 3 additions & 4 deletions src/components/courses/CourseForm.test.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as React from 'react'
import { mount, shallow } from 'enzyme'
import { shallow, mount, render } from 'enzyme'
import { should } from 'chai'

import { CourseForm, CourseFormProps } from './CourseForm'
Expand All @@ -24,9 +24,8 @@ function setup(saving: boolean) {
describe('CourseForm', () => {
it('renders form and h1', () => {
const wrapper = setup(false)
wrapper.find('form').should.equal(1)
wrapper.find('form').should.have.lengthOf(1)
wrapper.find('h1').text().should.equal('Manage Course')
wrapper.find('input').props().value.should.equal('Saving...')
})

it('save button is labeled "Save" when not saving', () => {
Expand All @@ -35,7 +34,7 @@ describe('CourseForm', () => {
})

it('save button is labeled "Saving..." when saving', () => {
const wrapper = setup(false)
const wrapper = setup(true)
wrapper.find('input').props().value.should.equal('Saving...')
})
})

0 comments on commit 4b34083

Please sign in to comment.