feat: Assign `scope` option to $scope

Event tests passing
ts
Jason Staten 5 years ago
parent 79c6c4f2a5
commit b69df8d8e7

@ -1,4 +1,3 @@
import React from 'react'
import {render, fireEvent} from '../' import {render, fireEvent} from '../'
const eventTypes = [ const eventTypes = [
@ -43,7 +42,7 @@ const eventTypes = [
events: [ events: [
'click', 'click',
'contextMenu', 'contextMenu',
'doubleClick', 'dblClick',
'drag', 'drag',
'dragEnd', 'dragEnd',
'dragEnter', 'dragEnter',
@ -131,48 +130,56 @@ const eventTypes = [
eventTypes.forEach(({type, events, elementType, init}) => { eventTypes.forEach(({type, events, elementType, init}) => {
describe(`${type} Events`, () => { describe(`${type} Events`, () => {
events.forEach(eventName => { events.forEach(eventName => {
const propName = `on${eventName.charAt(0).toUpperCase()}${eventName.slice( const propName = eventName.toLowerCase()
1,
)}`
it(`triggers ${propName}`, () => { it(`triggers ${eventName}`, () => {
const ref = React.createRef()
const spy = jest.fn() const spy = jest.fn()
render( const {getByTestId} = render(
React.createElement(elementType, { `
[propName]: spy, <${elementType}
ref, data-testid="target"
}), ng-on-${propName}="spy()"
></${elementType}>`,
{
scope: {
spy,
},
},
) )
fireEvent[eventName](ref.current, init) const target = getByTestId('target')
fireEvent[eventName](target, init)
expect(spy).toHaveBeenCalledTimes(1) expect(spy).toHaveBeenCalledTimes(1)
}) })
}) })
}) })
}) })
test('onChange works', () => {
const handleChange = jest.fn()
const {
container: {firstChild: input},
} = render(<input onChange={handleChange} />)
fireEvent.change(input, {target: {value: 'a'}})
expect(handleChange).toHaveBeenCalledTimes(1)
})
test('calling `fireEvent` directly works too', () => { test('calling `fireEvent` directly works too', () => {
const handleEvent = jest.fn() const spy = jest.fn()
const {
container: {firstChild: button}, const {getByTestId} = render(
} = render(<button onClick={handleEvent} />) `
<button
data-testid="target"
ng-click="spy()"
></button>`,
{
scope: {
spy,
},
},
)
const target = getByTestId('target')
fireEvent( fireEvent(
button, target,
new Event('MouseEvent', { new Event('click', {
bubbles: true, bubbles: true,
cancelable: true, cancelable: true,
button: 0, button: 0,
}), }),
) )
expect(spy).toHaveBeenCalledTimes(1)
}) })

@ -29,3 +29,15 @@ test('supports fragments', () => {
const {asFragment} = render(`<atl-fragment></atl-fragment>`) const {asFragment} = render(`<atl-fragment></atl-fragment>`)
expect(asFragment()).toMatchSnapshot() expect(asFragment()).toMatchSnapshot()
}) })
test('assigns to scope', () => {
const {getByText} = render(`<div>Hello {{name}}</div>`, {
scope: {
name: 'World',
},
})
expect(() => getByText('Hello World')).not.toThrow(
'Unable to find an element with the text: Hello World.',
)
})

@ -9,7 +9,7 @@ import {
const mountedContainers = new Set() const mountedContainers = new Set()
const mountedScopes = new Set() const mountedScopes = new Set()
function render(ui, {container, baseElement = container, queries} = {}) { function render(ui, {container, baseElement = container, queries, scope} = {}) {
if (!baseElement) { if (!baseElement) {
// default to document.body instead of documentElement to avoid output of potentially-large // default to document.body instead of documentElement to avoid output of potentially-large
// head elements (such as JSS style blocks) in debug output // head elements (such as JSS style blocks) in debug output
@ -31,6 +31,7 @@ function render(ui, {container, baseElement = container, queries} = {}) {
($compile, $rootScope) => { ($compile, $rootScope) => {
$scope = $rootScope.$new() $scope = $rootScope.$new()
mountedScopes.add($scope) mountedScopes.add($scope)
Object.assign($scope, scope)
const element = $compile(ui)($scope)[0] const element = $compile(ui)($scope)[0]
container.appendChild(element) container.appendChild(element)
@ -93,5 +94,13 @@ Object.keys(dtlFireEvent).forEach(key => {
} }
}) })
fireEvent.mouseEnter = (...args) => {
return dtlFireEvent.mouseOver(...args)
}
fireEvent.mouseLeave = (...args) => {
return dtlFireEvent.mouseOut(...args)
}
export * from '@testing-library/dom' export * from '@testing-library/dom'
export {render, cleanup, fireEvent} export {render, cleanup, fireEvent}

Loading…
Cancel
Save