Skip to content
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

chore: update all packages and get 100% coverage #118

Merged
merged 1 commit into from
Jul 8, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 9 additions & 6 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
const jestConfig = require('kcd-scripts/jest')
const config = require('kcd-scripts/jest')

module.exports = Object.assign(jestConfig, {
testEnvironment: 'jest-environment-node',
testURL: 'http://localhost/',
setupTestFrameworkScriptFile: '<rootDir>/setupTests.js',
})
module.exports = {
...config,
projects: [
require.resolve('jest-watch-select-projects'),
require.resolve('./tests/jest.config.dom'),
require.resolve('./tests/jest.config.node'),
],
}
17 changes: 11 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,14 @@
"build": "kcd-scripts build",
"lint": "kcd-scripts lint",
"test": "kcd-scripts test",
"test:all": "npm test && npm test -- --env jsdom",
"test:update": "npm test -- --updateSnapshot --coverage",
"validate": "kcd-scripts validate build,lint,test:all",
"setup": "npm install && npm run validate -s",
"precommit": "kcd-scripts precommit"
"validate": "kcd-scripts validate",
"setup": "npm install && npm run validate -s"
},
"husky": {
"hooks": {
"pre-commit": "kcd-scripts pre-commit"
}
},
"files": [
"dist",
Expand All @@ -33,18 +36,20 @@
"author": "Ernesto Garcia <gnapse@gmail.com> (http://gnapse.github.io/)",
"license": "MIT",
"dependencies": {
"@babel/runtime": "^7.5.1",
"chalk": "^2.4.1",
"css": "^2.2.3",
"css.escape": "^1.5.1",
"jest-diff": "^24.0.0",
"jest-matcher-utils": "^24.0.0",
"lodash": "^4.17.11",
"pretty-format": "^24.0.0",
"redent": "^2.0.0"
"redent": "^3.0.0"
},
"devDependencies": {
"jest-watch-select-projects": "^0.1.2",
"jsdom": "^15.1.0",
"kcd-scripts": "^0.44.0"
"kcd-scripts": "^1.4.0"
},
"eslintConfig": {
"extends": "./node_modules/kcd-scripts/eslint.js",
Expand Down
7 changes: 5 additions & 2 deletions src/__tests__/to-be-in-the-document.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import document from './helpers/document'
import {HtmlElementTypeError} from '../utils'
import document from './helpers/document'

test('.toBeInTheDocument', () => {
document.body.innerHTML = `
Expand Down Expand Up @@ -33,10 +33,13 @@ test('.toBeInTheDocument', () => {
expect(() => expect(fakeElement).toBeInTheDocument()).toThrowError(
HtmlElementTypeError,
)
expect(() => expect(nullElement).toBeInTheDocument()).toThrowError(
HtmlElementTypeError,
)
expect(() => expect(undefinedElement).toBeInTheDocument()).toThrowError(
HtmlElementTypeError,
)
expect(() => expect(nullElement).toBeInTheDocument()).toThrowError(
expect(() => expect(undefinedElement).not.toBeInTheDocument()).toThrowError(
HtmlElementTypeError,
)
})
82 changes: 82 additions & 0 deletions src/__tests__/to-have-value.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,4 +103,86 @@ describe('.toHaveValue', () => {
expect(queryByTestId('radio')).toHaveValue('')
}).toThrow()
})

test('throws when the expected input value does not match', () => {
const {container} = render(`<input data-testid="one" value="foo" />`)
const input = container.firstChild
let errorMessage
try {
expect(input).toHaveValue('something else')
} catch (error) {
errorMessage = error.message
}

expect(errorMessage).toMatchInlineSnapshot(`
"<dim>expect(</><red>element</><dim>).toHaveValue(</><green>something else</><dim>)</>

Expected the element to have value:
<green> something else</>
Received:
<red> foo</>"
`)
})

test('throws when using not but the expected input value does match', () => {
const {container} = render(`<input data-testid="one" value="foo" />`)
const input = container.firstChild
let errorMessage

try {
expect(input).not.toHaveValue('foo')
} catch (error) {
errorMessage = error.message
}
expect(errorMessage).toMatchInlineSnapshot(`
"<dim>expect(</><red>element</><dim>).not.toHaveValue(</><green>foo</><dim>)</>

Expected the element not to have value:
<green> foo</>
Received:
<red> foo</>"
`)
})

test('throws when the form has no a value but a value is expected', () => {
const {container} = render(`<input data-testid="one" />`)
const input = container.firstChild
let errorMessage

try {
expect(input).toHaveValue()
} catch (error) {
errorMessage = error.message
}
expect(errorMessage).toMatchInlineSnapshot(`
"<dim>expect(</><red>element</><dim>).toHaveValue(</><green>expected</><dim>)</>

Expected the element to have value:
<green> (any)</>
Received:
"
`)
})

test('throws when the form has a value but none is expected', () => {
const {container} = render(`<input data-testid="one" value="foo" />`)
const input = container.firstChild
let errorMessage

try {
expect(input).not.toHaveValue()
} catch (error) {
errorMessage = error.message
}
expect(errorMessage).toMatchInlineSnapshot(`
"<dim>expect(</><red>element</><dim>).not.toHaveValue(</><green>expected</><dim>)</>

Expected the element not to have value:
<green> (any)</>
Received:
<red> foo</>"
`)
})
})

/* eslint max-lines-per-function:0 */
15 changes: 15 additions & 0 deletions src/__tests__/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,19 @@ describe('checkHtmlElement', () => {
checkHtmlElement(() => {}, () => {}, {})
}).toThrow(HtmlElementTypeError)
})

it('throws for almost element-like objects', () => {
class FakeObject {}
expect(() => {
checkHtmlElement(
{
ownerDocument: {
defaultView: {HTMLElement: FakeObject, SVGElement: FakeObject},
},
},
() => {},
{},
)
}).toThrow(HtmlElementTypeError)
})
})
2 changes: 1 addition & 1 deletion src/to-be-in-the-document.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export function toBeInTheDocument(element) {
'',
receivedColor(`${stringify(element.ownerDocument.cloneNode(false))} ${
this.isNot ? 'contains:' : 'does not contain:'
} ${stringify(element ? element.cloneNode(false) : element)}
} ${stringify(element.cloneNode(false))}
`),
].join('\n')
},
Expand Down
9 changes: 9 additions & 0 deletions tests/jest.config.dom.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const path = require('path')
const config = require('kcd-scripts/jest')

module.exports = {
rootDir: path.resolve(__dirname, '..'),
displayName: 'jsdom',
testEnvironment: 'dom',
...config,
}
9 changes: 9 additions & 0 deletions tests/jest.config.node.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const path = require('path')
const config = require('kcd-scripts/jest')

module.exports = {
rootDir: path.resolve(__dirname, '..'),
displayName: 'node',
testEnvironment: 'node',
...config,
}
2 changes: 1 addition & 1 deletion setupTests.js → tests/setup-env.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {plugins} from 'pretty-format'
import './src/extend-expect'
import '../src/extend-expect'

expect.addSnapshotSerializer(plugins.ConvertAnsi)