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

Added tests for mithril #11

Merged
merged 1 commit into from
Sep 17, 2018
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
33 changes: 33 additions & 0 deletions mithril.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import 'jest-dom/extend-expect'
import m from 'mithril'
import {getQueriesForElement, fireEvent, wait} from 'dom-testing-library'

const Counter = () => {
let count = 0
return {
view: () =>
m(
'button',
{
onclick: () => {
count++
},
},
count,
),
}
}

// tests:
test('counter increments', async () => {
const div = document.createElement('div')
m.mount(div, Counter)

const {getByText} = getQueriesForElement(div)
const counter = getByText('0')
fireEvent.click(counter)
await wait(() => expect(counter).toHaveTextContent('1'))
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of using wait here, could you use fireEventAsync (see hyperapp.test.js for an example).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's what I tried to begin with, but the test suite still fails. Here's the code:

test('renders a counter', async () => {
  const div = document.createElement('div')
  m.mount(div, Counter)

  const {getByText} = getQueriesForElement(div)
  const counter = getByText('0')
  await fireEventAsync.click(counter)
  expect(counter).toHaveTextContent('1')

  await fireEventAsync.click(counter)
  expect(counter).toHaveTextContent('2')
})

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Weird. Maybe it takes longer than a single tick of the event loop to rerender for some reason. Ok, thanks!


fireEvent.click(counter)
await wait(() => expect(counter).toHaveTextContent('2'))
})
5 changes: 5 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"hyperapp": "^1.2.9",
"jquery": "^3.3.1",
"knockout": "^3.4.2",
"mithril": "^1.1.6",
"preact": "^8.3.1",
"react": "^16.5.1",
"stimulus": "^1.1.0",
Expand Down