diff --git a/src/__tests__/act.js b/src/__tests__/act.js deleted file mode 100644 index 2adcee9..0000000 --- a/src/__tests__/act.js +++ /dev/null @@ -1,45 +0,0 @@ -import React from 'react' -import {render, fireEvent} from '../' - -test('render calls useEffect immediately', () => { - const effectCb = jest.fn() - function MyUselessComponent() { - React.useEffect(effectCb) - return null - } - render() - expect(effectCb).toHaveBeenCalledTimes(1) -}) - -test('findByTestId returns the element', async () => { - const ref = React.createRef() - const {findByTestId} = render(
) - expect(await findByTestId('foo')).toBe(ref.current) -}) - -test('fireEvent triggers useEffect calls', () => { - const effectCb = jest.fn() - function Counter() { - React.useEffect(effectCb) - const [count, setCount] = React.useState(0) - return - } - const { - container: {firstChild: buttonNode}, - } = render() - - effectCb.mockClear() - fireEvent.click(buttonNode) - expect(buttonNode).toHaveTextContent('1') - expect(effectCb).toHaveBeenCalledTimes(1) -}) - -test('calls to hydrate will run useEffects', () => { - const effectCb = jest.fn() - function MyUselessComponent() { - React.useEffect(effectCb) - return null - } - render(, {hydrate: true}) - expect(effectCb).toHaveBeenCalledTimes(1) -}) diff --git a/src/__tests__/cleanup.js b/src/__tests__/cleanup.js index ec2a057..f061168 100644 --- a/src/__tests__/cleanup.js +++ b/src/__tests__/cleanup.js @@ -1,28 +1,33 @@ -import React from 'react' +import angular from 'angular' +import 'angular-mocks' import {render, cleanup} from '../' +beforeEach(() => { + angular.module('atl', []) + angular.mock.module('atl') +}) + test('cleans up the document', () => { const spy = jest.fn() - const divId = 'my-div' - - class Test extends React.Component { - componentWillUnmount() { - expect(document.getElementById(divId)).toBeInTheDocument() - spy() - } + angular.module('atl').component('atlCleanup', { + template: `
`, + controller: class { + divId = 'my-div' - render() { - return
- } - } + $onDestroy() { + expect(document.getElementById(this.divId)).toBeInTheDocument() + spy() + } + }, + }) - render() + render(``) cleanup() expect(document.body.innerHTML).toBe('') expect(spy).toHaveBeenCalledTimes(1) }) test('cleanup does not error when an element is not a child', () => { - render(
, {container: document.createElement('div')}) + render(`
`, {container: document.createElement('div')}) cleanup() }) diff --git a/src/pure.js b/src/pure.js index 589e20c..f7664b1 100644 --- a/src/pure.js +++ b/src/pure.js @@ -7,6 +7,7 @@ import { } from '@testing-library/dom' const mountedContainers = new Set() +const mountedScopes = new Set() function render(ui, {container, baseElement = container, queries} = {}) { if (!baseElement) { @@ -29,6 +30,7 @@ function render(ui, {container, baseElement = container, queries} = {}) { '$rootScope', ($compile, $rootScope) => { $scope = $rootScope.$new() + mountedScopes.add($scope) const element = $compile(ui)($scope)[0] container.appendChild(element) @@ -64,6 +66,7 @@ function render(ui, {container, baseElement = container, queries} = {}) { } function cleanup() { + mountedScopes.forEach(cleanupScope) mountedContainers.forEach(cleanupAtContainer) } @@ -76,6 +79,10 @@ function cleanupAtContainer(container) { mountedContainers.delete(container) } +function cleanupScope(scope) { + scope.$destroy() +} + function fireEvent(...args) { return dtlFireEvent(...args) }