-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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 |
---|---|---|
@@ -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() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think people will look to these tests as an example of how to test their components with good practices. With that in mind, let's do one of the following:
I don't really care which we do. |
||
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', | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Rather than calling it "dom" call it "element." The Same below. |
||
pass: true, | ||
} | ||
} else { | ||
return { | ||
message: () => 'expected the dom not to be present', | ||
pass: false, | ||
} | ||
} | ||
}, | ||
|
||
toHaveTextContent(htmlElement, checkWith) { | ||
const textContent = htmlElement.textContent | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could we add an extra check here that throw new Error(`The given subject is a ${getDisplayName(htmlElement)}, not an HTMLElement`) Or return an object, but this should probably fail regardless of whether people use The function getDisplayName(subject) {
if (subject && subject.constructor) {
return subject.constructor.name
} else {
return typeof subject
}
} Or, maybe there's a better thing for this available on npm... There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I guess the code that you had mentioned |
||
const pass = textContent === checkWith | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could we extend this to use the Using the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, thats a good idea. Can take care of that, will move
|
||
if (pass) { | ||
return { | ||
message: () => `expected ${textContent} not equals to ${checkWith}`, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's use |
||
pass: true, | ||
} | ||
} else { | ||
return { | ||
message: () => `expected ${textContent} equals to ${checkWith}`, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's use |
||
pass: false, | ||
} | ||
} | ||
}, | ||
|
||
toSatisfyDOM(element, fn) { | ||
const pass = fn(element) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this should be more like this: https://github.com/jest-community/jest-extended/blob/master/src/matchers/toSatisfy/index.js In fact, it looks like we should probably be changing everything in this file to use |
||
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 theeslintConfig
to have arules
property that disables this rule 👍