From 78021a6ab00e5d5bad113ab3001c2889d9dd40f3 Mon Sep 17 00:00:00 2001 From: Jason Staten Date: Fri, 1 Nov 2019 21:01:53 -0600 Subject: [PATCH] feat: call $destroy on cleanup --- src/__tests__/act.js | 45 ---------------------------------------- src/__tests__/cleanup.js | 33 ++++++++++++++++------------- src/pure.js | 7 +++++++ 3 files changed, 26 insertions(+), 59 deletions(-) delete mode 100644 src/__tests__/act.js 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) } -- 2.45.2