rsnext/test/unit/mitt.test.ts

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

84 lines
1.6 KiB
TypeScript
Raw Normal View History

/* eslint-env jest */
import mitt from 'next/dist/shared/lib/mitt'
2017-07-09 05:46:53 +02:00
describe('mitt', () => {
2017-07-09 05:46:53 +02:00
describe('With listeners', () => {
(Fix)(Tests)Fix typo in unit testing (#47644) <!-- Thanks for opening a PR! Your contribution is much appreciated. To make sure your PR is handled as smoothly as possible we request that you follow the checklist sections below. Choose the right checklist for the change(s) that you're making: ## For Contributors ### Improving Documentation or adding/fixing Examples - The "examples guidelines" are followed from our contributing doc https://github.com/vercel/next.js/blob/canary/contributing/examples/adding-examples.md - Make sure the linting passes by running `pnpm build && pnpm lint`. See https://github.com/vercel/next.js/blob/canary/contributing/repository/linting.md ### Fixing a bug - Related issues linked using `fixes #number` - Tests added. See: https://github.com/vercel/next.js/blob/canary/contributing/core/testing.md#writing-tests-for-nextjs - Errors have a helpful link attached, see https://github.com/vercel/next.js/blob/canary/contributing.md ### Adding a feature - Implements an existing feature request or RFC. Make sure the feature request has been accepted for implementation before opening a PR. (A discussion must be opened, see https://github.com/vercel/next.js/discussions/new?category=ideas) - Related issues/discussions are linked using `fixes #number` - e2e tests added (https://github.com/vercel/next.js/blob/canary/contributing/core/testing.md#writing-tests-for-nextjs - Documentation added - Telemetry added. In case of a feature if it's used or not. - Errors have a helpful link attached, see https://github.com/vercel/next.js/blob/canary/contributing.md ## For Maintainers - Minimal description (aim for explaining to someone not on the team to understand the PR) - When linking to a Slack thread, you might want to share details of the conclusion - Link both the Linear (Fixes NEXT-xxx) and the GitHub issues - Add review comments if necessary to explain to the reviewer the logic behind a change ### What? ### Why? ### How? Closes NEXT- Fixes # --> Fix Typo In Unit Testing
2023-04-02 07:23:15 +02:00
it('should listen to an event', () => {
2020-05-18 21:24:37 +02:00
return new Promise((resolve) => {
2020-05-18 19:16:07 +02:00
const ev = mitt()
ev.on('sample', resolve)
ev.emit('sample')
})
2017-07-09 05:46:53 +02:00
})
it('should listen to multiple listeners', () => {
const ev = mitt()
2017-07-09 05:46:53 +02:00
let cnt = 0
ev.on('sample', () => {
cnt += 1
})
ev.on('sample', () => {
cnt += 1
})
2017-07-09 05:46:53 +02:00
ev.emit('sample')
expect(cnt).toBe(2)
})
it('should listen to multiple events', () => {
const ev = mitt()
2017-07-09 05:46:53 +02:00
const data = []
2020-05-18 21:24:37 +02:00
const cb = (name) => {
data.push(name)
}
2017-07-09 05:46:53 +02:00
ev.on('sample1', cb)
ev.on('sample2', cb)
ev.emit('sample1', 'one')
ev.emit('sample2', 'two')
expect(data).toEqual(['one', 'two'])
})
it('should support multiple arguments', () => {
const ev = mitt()
2017-07-09 05:46:53 +02:00
let data
ev.on('sample', (...args) => {
data = args
})
2017-07-09 05:46:53 +02:00
ev.emit('sample', 'one', 'two')
expect(data).toEqual(['one', 'two'])
})
it('should possible to stop listening an event', () => {
const ev = mitt()
2017-07-09 05:46:53 +02:00
let cnt = 0
const cb = () => {
cnt += 1
}
2017-07-09 05:46:53 +02:00
ev.on('sample', cb)
ev.emit('sample')
expect(cnt).toBe(1)
ev.off('sample', cb)
ev.emit('sample')
expect(cnt).toBe(1)
})
})
describe('Without a listener', () => {
it('should not fail to emit', () => {
const ev = mitt()
2017-07-09 05:46:53 +02:00
ev.emit('aaaa', 10, 20)
})
})
})