-
Notifications
You must be signed in to change notification settings - Fork 156
feat(commands): use waitForElement; add jest & cypress tests #2
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
Merged
kentcdodds
merged 7 commits into
testing-library:master
from
sompylasar:pr/waitforelement-and-tests
Apr 10, 2018
Merged
Changes from 6 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
f4bba5a
feat(commands): use waitForElement; add jest & cypress tests
sompylasar 9c56cd8
fix(ci): attempt to use `[` instead of `[[` for bash test tool
sompylasar 65f004c
fix(package): add missing `fkill-cli` needed by npm scripts
sompylasar d9411b9
Update package.json
6fcf565
Update cypress.json
4ef82df
Update commands.spec.js
0ac00a3
Update package.json
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,5 @@ | ||
{ | ||
"baseUrl": "http://localhost:13370", | ||
"videoRecording": false, | ||
"screenshotOnHeadlessFailure": false | ||
} |
This file contains hidden or 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 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="UTF-8"> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | ||
<meta http-equiv="X-UA-Compatible" content="ie=edge"> | ||
<title>cypress-testing-library</title> | ||
<style> | ||
blockquote { | ||
margin: 0; | ||
border-left: 4px solid grey; | ||
padding-left: 10px; | ||
color: grey; | ||
} | ||
section { | ||
padding: 10px; | ||
} | ||
</style> | ||
</head> | ||
<body> | ||
<blockquote> | ||
No auto-reload after changing this static HTML markup: | ||
click <span title="Run All Tests">↻</span> Run All Tests. | ||
</blockquote> | ||
<section> | ||
<h2>getByPlaceholderText</h2> | ||
<input type="text" placeholder="Placeholder Text" /> | ||
</section> | ||
<section> | ||
<h2>getByText</h2> | ||
<button onclick="this.innerText = 'Button Clicked'">Button Text</button> | ||
</section> | ||
<section> | ||
<h2>getByLabelText</h2> | ||
<label for="input-labelled-by-id">Label For Input Labelled By Id</label> | ||
<input type="text" placeholder="Input Labelled By Id" id="input-labelled-by-id" /> | ||
</section> | ||
<section> | ||
<h2>getByAltText</h2> | ||
<img | ||
src="data:image/png;base64," | ||
alt="Image Alt Text" | ||
onclick="this.style.border = '5px solid red'" | ||
/> | ||
</section> | ||
<section> | ||
<h2>getByTestId</h2> | ||
<img | ||
data-testid="image-with-random-alt-tag" | ||
src="data:image/png;base64," | ||
onclick="this.style.border = '5px solid red'" | ||
/> | ||
</section> | ||
<!-- Prettier unindents the script tag below --> | ||
<script> | ||
document | ||
.querySelector('[data-testid="image-with-random-alt-tag"]') | ||
.setAttribute('alt', 'Image Random Alt Text ' + Math.random()) | ||
</script> | ||
</body> | ||
</html> |
This file contains hidden or 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,38 @@ | ||
describe('dom-testing-library commands', () => { | ||
beforeEach(() => { | ||
cy.visit('/') | ||
}) | ||
it('getByPlaceholderText', () => { | ||
cy | ||
.getByPlaceholderText('Placeholder Text') | ||
.click() | ||
.type('Hello Placeholder') | ||
}) | ||
|
||
it('getByText', () => { | ||
cy | ||
.getByText('Button Text') | ||
.click() | ||
}) | ||
|
||
it('getByLabelText', () => { | ||
cy | ||
.getByLabelText('Label For Input Labelled By Id') | ||
.click() | ||
.type('Hello Input Labelled By Id') | ||
}) | ||
|
||
it('getByAltText', () => { | ||
cy | ||
.getByAltText('Image Alt Text') | ||
.click() | ||
}) | ||
|
||
it('getByTestId', () => { | ||
cy | ||
.getByTestId('image-with-random-alt-tag') | ||
.click() | ||
}) | ||
}) | ||
|
||
/* global cy */ |
This file contains hidden or 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,2 @@ | ||
// Keeping this file here, otherwise it gets recreated by Cypress on each run. | ||
module.exports = () => {} |
This file contains hidden or 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 @@ | ||
import '../../src/add-commands' |
This file contains hidden or 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,5 @@ | ||
const jestConfig = require('kcd-scripts/jest') | ||
|
||
module.exports = Object.assign(jestConfig, { | ||
testEnvironment: 'jest-environment-jsdom', | ||
}) |
This file contains hidden or 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 |
---|---|---|
|
@@ -10,9 +10,14 @@ | |
"add-contributor": "kcd-scripts contributors add", | ||
"build": "kcd-scripts build", | ||
"lint": "kcd-scripts lint", | ||
"test": "echo TODO", | ||
"test:ci": "echo TODO", | ||
"test:update": "npm test -- --updateSnapshot --coverage", | ||
"test": "npm-run-all --parallel test:unit test:cypress:ci", | ||
"test:unit": "kcd-scripts test --no-watch", | ||
"test:unit:watch": "kcd-scripts test", | ||
"test:cypress:serve": "serve --clipless --local --port 13370 ./cypress/fixtures/test-app", | ||
"test:cypress:run": "cypress run", | ||
"test:cypress:open": "cypress open", | ||
"test:cypress": "npm-run-all --silent --parallel --race test:cypress:serve test:cypress:run", | ||
"test:cypress:dev": "npm-run-all --silent --parallel --race test:cypress:serve test:cypress:open", | ||
"validate": "kcd-scripts validate build,lint,test", | ||
"setup": "npm install && npm run validate -s", | ||
"precommit": "kcd-scripts precommit" | ||
|
@@ -34,16 +39,23 @@ | |
"author": "Kent C. Dodds <[email protected]> (http://kentcdodds.com/)", | ||
"license": "MIT", | ||
"dependencies": { | ||
"dom-testing-library": "^1.0.0" | ||
"dom-testing-library": "^1.3.0" | ||
}, | ||
"devDependencies": { | ||
"kcd-scripts": "^0.36.1" | ||
"cypress": "^2.1.0", | ||
"kcd-scripts": "^0.37.0", | ||
"npm-run-all": "^4.1.2", | ||
"serve": "^6.5.4" | ||
}, | ||
"peerDependencies": { | ||
"cypress": "^2.1.0" | ||
}, | ||
"eslintConfig": { | ||
"extends": "./node_modules/kcd-scripts/eslint.js" | ||
"extends": "./node_modules/kcd-scripts/eslint.js", | ||
"rules": { | ||
"import/prefer-default-export": "off", | ||
"import/no-unassigned-import": "off" | ||
} | ||
}, | ||
"eslintIgnore": [ | ||
"node_modules", | ||
|
This file contains hidden or 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,11 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`exports expected commands 1`] = ` | ||
Array [ | ||
"getByPlaceholderText", | ||
"getByText", | ||
"getByLabelText", | ||
"getByAltText", | ||
"getByTestId", | ||
] | ||
`; |
This file contains hidden or 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,19 @@ | ||
import {commands} from '../' | ||
|
||
test('adds commands to Cypress', () => { | ||
const addMock = jest.fn().mockName('Cypress.Commands.add') | ||
global.Cypress = {Commands: {add: addMock}} | ||
global.cy = {} | ||
|
||
require('../add-commands') | ||
|
||
expect(addMock).toHaveBeenCalledTimes(commands.length) | ||
commands.forEach(({name}, index) => { | ||
expect(addMock.mock.calls[index]).toMatchObject([ | ||
name, | ||
// We get a new function that is `command.bind(null, cy)` i.e. global `cy` passed into the first argument. | ||
// The commands themselves will be tested separately in the Cypress end-to-end tests. | ||
expect.any(Function), | ||
]) | ||
}) | ||
}) |
This file contains hidden or 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,14 @@ | ||
import {commands} from '../' | ||
|
||
test('exports expected commands', () => { | ||
expect(commands).toMatchObject(expect.any(Array)) | ||
expect(commands.map(({name}) => name)).toMatchSnapshot() | ||
commands.forEach(command => | ||
expect(command).toMatchObject({ | ||
name: expect.any(String), | ||
// We get a new function that wraps the respective query from `dom-testing-library`. | ||
// The commands themselves will be tested separately in the Cypress end-to-end tests. | ||
command: expect.any(Function), | ||
}), | ||
) | ||
}) |
This file contains hidden or 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 |
---|---|---|
@@ -1,7 +1,7 @@ | ||
import {commands} from './' | ||
|
||
commands.forEach(({name, command}) => { | ||
Cypress.Commands.add(name, command) | ||
Cypress.Commands.add(name, command.bind(null, cy)) | ||
}) | ||
|
||
/* global Cypress */ | ||
/* global Cypress, cy */ |
This file contains hidden or 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 |
---|---|---|
@@ -1,44 +1,27 @@ | ||
import {queries} from 'dom-testing-library' | ||
import {queries, waitForElement} from 'dom-testing-library' | ||
|
||
const commands = Object.keys(queries) | ||
.filter(queryName => queryName.startsWith('getBy')) | ||
.map(queryName => { | ||
return { | ||
name: queryName, | ||
command: (...args) => { | ||
const fn = new Function( | ||
'args', | ||
'query', | ||
'getCommandWaiter', | ||
command: (cy, ...args) => { | ||
const queryImpl = queries[queryName] | ||
const commandImpl = doc => | ||
waitForElement(() => queryImpl(doc, ...args), {container: doc}) | ||
const thenHandler = new Function( | ||
'commandImpl', | ||
` | ||
return function Command__${queryName}({document}) { | ||
return getCommandWaiter(document, () => query(document, ...args))(); | ||
}; | ||
return function Command__${queryName}(thenArgs) { | ||
return commandImpl(thenArgs.document) | ||
} | ||
`, | ||
)(args, queries[queryName], getCommandWaiter) | ||
return cy.window({log: false}).then(fn) | ||
)(commandImpl) | ||
return cy.window({log: false}).then(thenHandler) | ||
}, | ||
} | ||
}) | ||
|
||
function getCommandWaiter(container, fn) { | ||
return function waiter() { | ||
const val = fn() | ||
if (val) { | ||
return val | ||
} else { | ||
return new Promise(resolve => { | ||
const observer = new MutationObserver(() => { | ||
observer.disconnect() | ||
resolve(waiter()) | ||
}) | ||
observer.observe(container, {subtree: true, childList: true}) | ||
}) | ||
} | ||
} | ||
} | ||
|
||
export {commands, getCommandWaiter} | ||
export {commands} | ||
|
||
/* eslint no-new-func:0, import/default:0 */ | ||
/* global cy */ | ||
/* eslint no-new-func:0 */ |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, this is super annoying...