-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Improving api #13
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
Improving api #13
Changes from 4 commits
5a2c05f
dd727d0
485e8fb
96a1a9e
cc2a1c3
3931a69
3ae81f6
987c4f3
57fd424
d1157a2
0cdb02b
74d8205
8348852
492645b
e222206
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| const expect = require('expect') //eslint-disable-line import/no-extraneous-dependencies | ||
| const extensions = require('./dist/jest-extensions') | ||
|
|
||
| const {toBeInTheDOM, toHaveTextContent, toSatisfyDOM} = extensions.default | ||
| expect.extend({toBeInTheDOM, toHaveTextContent, toSatisfyDOM}) | ||
|
|
||
| module.exports = expect |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -20,7 +20,8 @@ | |
| }, | ||
| "files": [ | ||
| "dist", | ||
| "typings" | ||
| "typings", | ||
| "extend-expect.js" | ||
| ], | ||
| "keywords": [], | ||
| "author": "Kent C. Dodds <[email protected]> (http://kentcdodds.com/)", | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,7 @@ | ||
| import React from 'react' | ||
| import axiosMock from 'axios' | ||
| import {render, Simulate, flushPromises} from '../' | ||
| import '../../extend-expect' //eslint-disable-line import/no-unassigned-import | ||
|
|
||
| // instead of importing it, we'll define it inline here | ||
| // import Fetch from '../fetch' | ||
|
|
@@ -37,14 +38,23 @@ test('Fetch makes an API call and displays the greeting when load-greeting is cl | |
| }), | ||
| ) | ||
| const url = '/greeting' | ||
| const {getByTestId, container} = render(<Fetch url={url} />) | ||
| const {getByTestId, container, queryByTestId} = render(<Fetch url={url} />) | ||
|
|
||
| // Act | ||
| Simulate.click(getByTestId('load-greeting')) | ||
|
|
||
| await flushPromises() | ||
|
|
||
| // Assert | ||
| expect(queryByTestId('foo')).not.toBeInTheDOM() | ||
|
||
| expect(queryByTestId('greeting-text')).toBeInTheDOM() | ||
| expect(queryByTestId('greeting-text')).toHaveTextContent('hello there') | ||
| expect(getByTestId('greeting-text')).toSatisfyDOM( | ||
| el => el.textContent === 'hello there', | ||
| ) | ||
| expect(queryByTestId('greeting-text')).not.toHaveTextContent( | ||
| 'you are not there', | ||
| ) | ||
| expect(axiosMock.get).toHaveBeenCalledTimes(1) | ||
| expect(axiosMock.get).toHaveBeenCalledWith(url) | ||
| expect(getByTestId('greeting-text').textContent).toBe('hello there') | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| const extensions = { | ||
| toBeInTheDOM(received) { | ||
| if (received) { | ||
| return { | ||
| message: () => 'expected the dom to be present', | ||
|
||
| pass: true, | ||
| } | ||
| } else { | ||
| return { | ||
| message: () => 'expected the dom not to be present', | ||
| pass: false, | ||
| } | ||
| } | ||
| }, | ||
|
|
||
| toHaveTextContent(htmlElement, checkWith) { | ||
| const textContent = htmlElement.textContent | ||
|
||
| const pass = textContent === checkWith | ||
|
||
| if (pass) { | ||
| return { | ||
| message: () => `expected ${textContent} not equals to ${checkWith}`, | ||
|
||
| pass: true, | ||
| } | ||
| } else { | ||
| return { | ||
| message: () => `expected ${textContent} equals to ${checkWith}`, | ||
|
||
| pass: false, | ||
| } | ||
| } | ||
| }, | ||
|
|
||
| toSatisfyDOM(element, fn) { | ||
| const pass = fn(element) | ||
|
||
| if (pass) { | ||
| return { | ||
| message: () => 'expected condition not equals to true', | ||
| pass: true, | ||
| } | ||
| } else { | ||
| return { | ||
| message: () => 'expected condition equals to true', | ||
| pass: false, | ||
| } | ||
| } | ||
| }, | ||
| } | ||
|
|
||
| export default extensions | ||
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.
Let's just disable this rule globally. It really annoys me and I should remove it from my base config eventually...
In the
package.json, update theeslintConfigto have arulesproperty that disables this rule 👍