Skip to content

Commit ec93586

Browse files
committed
Handles Menu destroy
1 parent ce5ab5d commit ec93586

File tree

4 files changed

+37
-11
lines changed

4 files changed

+37
-11
lines changed

package-lock.json

+17
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
"eslint-plugin-jsx-a11y": "^6.6.1",
3232
"eslint-plugin-react": "^7.31.11",
3333
"jest": "^29.3.1",
34+
"jest-after-this": "^1.0.2",
3435
"jest-cli": "^29.3.1",
3536
"jest-environment-jsdom": "^29.4.2"
3637
},

src/Menu/Menu.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -248,10 +248,11 @@ export default class Menu extends AriaComponent {
248248
this.disclosures.forEach((disclosure) => {
249249
this.removeAttributes(disclosure.controller);
250250
this.removeAttributes(disclosure.target);
251+
252+
disclosure.controller.removeEventListener('click', this.controllerHandleClick);
253+
disclosure.controller.removeEventListener('focusout', this.constructor.controllerHandleFocusout);
251254
});
252255

253-
controller.removeEventListener('click', this.controllerHandleClick);
254-
controller.removeEventListener('focusout', this.constructor.controllerHandleFocusout);
255256
document.body.removeEventListener('keydown', this.bodyHandleKeydown);
256257
this.off('focusout', this.menuHandleFocusout);
257258
this.off('click', this.menuHandleClick);

src/Menu/Menu.test.js

+16-9
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
/* eslint-disable max-len */
2+
import { afterThis } from 'jest-after-this';
23
import user from '@/.jest/user';
34
import Menu from '.';
45

@@ -207,6 +208,12 @@ describe('The Menu should initialize as expected', () => {
207208
});
208209

209210
test('Clicking a menu link re-sets aria-current', async () => {
211+
afterThis(() => {
212+
// Clean up to reset attributes.
213+
menuLink.removeAttribute('aria-current');
214+
current.setAttribute('aria-current', 'page');
215+
});
216+
210217
const menuLink = document.querySelector('.sublist2-first-item');
211218
const current = document.querySelector('[aria-current="page"]');
212219

@@ -216,23 +223,23 @@ describe('The Menu should initialize as expected', () => {
216223
expect(current.getAttribute('aria-current')).toBeNull();
217224
});
218225

219-
test.skip('All attributes are removed from elements managed by the Menu', () => {
226+
test('All attributes are removed from elements managed by the Menu', () => {
220227
menu.destroy();
228+
expect(onDestroy).toHaveBeenCalledTimes(1);
221229

222230
expect(list.element).toBeUndefined();
223-
expect(onDestroy).toHaveBeenCalledTimes(1);
224231

225-
expect(sublistOne.getAttribute('aria-hidden')).toBeNull();
232+
expect(firstController.getAttribute('aria-expanded')).toBeNull();
233+
expect(secondController.getAttribute('aria-expanded')).toBeNull();
234+
expect(firstTarget.getAttribute('aria-hidden')).toBeNull();
235+
expect(secondTarget.getAttribute('aria-hidden')).toBeNull();
226236

227237
// Quick and dirty verification that the original markup is restored.
228238
expect(document.body.innerHTML).toEqual(menuMarkup);
229239

230-
expect(onDestroy).toHaveBeenCalledTimes(1);
231-
return Promise.resolve().then(() => {
232-
const { detail } = getEventDetails(onDestroy);
240+
const { detail } = getEventDetails(onDestroy);
233241

234-
expect(detail.element).toStrictEqual(list);
235-
expect(detail.instance).toStrictEqual(menu);
236-
});
242+
expect(detail.element).toStrictEqual(list);
243+
expect(detail.instance).toStrictEqual(menu);
237244
});
238245
});

0 commit comments

Comments
 (0)