Commit 8e7cd3ca authored by Robert Knight's avatar Robert Knight

Optimize readability of several tests

 - Rename `getElement` => `findElementByTestId` for clarity
 - Eliminate unnecessary `context` blocks in `MarkdownEditor` tests
 - Narrow scope of `console.warn` suppression in `useArrowKeyNavigation`
   test
 - Explain why keyboard navigation tests do not use Enzyme
parent ab16a393
...@@ -30,16 +30,10 @@ describe('shared/keyboard-navigation', () => { ...@@ -30,16 +30,10 @@ describe('shared/keyboard-navigation', () => {
container = document.createElement('div'); container = document.createElement('div');
document.body.append(container); document.body.append(container);
renderToolbar(); renderToolbar();
// Suppress "Add @babel/plugin-transform-react-jsx-source to get a more
// detailed component stack" warning in tests that trigger an error during
// effects.
sinon.stub(console, 'warn');
}); });
afterEach(() => { afterEach(() => {
container.remove(); container.remove();
console.warn.restore();
}); });
// Workaround for an issue with `useEffect` throwing exceptions during // Workaround for an issue with `useEffect` throwing exceptions during
...@@ -53,20 +47,19 @@ describe('shared/keyboard-navigation', () => { ...@@ -53,20 +47,19 @@ describe('shared/keyboard-navigation', () => {
}); });
function renderToolbar(options = {}) { function renderToolbar(options = {}) {
// We render the component with Preact directly rather than using Enzyme
// for these tests. Since the `tabIndex` state lives only in the DOM,
// and there are no child components involved, this is more convenient.
act(() => { act(() => {
render(<Toolbar navigationOptions={options} />, container); render(<Toolbar navigationOptions={options} />, container);
}); });
return getElement('toolbar'); return findElementByTestId('toolbar');
} }
function getElement(testId) { function findElementByTestId(testId) {
return container.querySelector(`[data-testid=${testId}]`); return container.querySelector(`[data-testid=${testId}]`);
} }
function getElements() {
return Array.from(getElement('toolbar').querySelectorAll('a,button'));
}
function pressKey(key) { function pressKey(key) {
const event = new KeyboardEvent('keydown', { const event = new KeyboardEvent('keydown', {
bubbles: true, bubbles: true,
...@@ -74,7 +67,7 @@ describe('shared/keyboard-navigation', () => { ...@@ -74,7 +67,7 @@ describe('shared/keyboard-navigation', () => {
key, key,
}); });
act(() => { act(() => {
getElement('toolbar').dispatchEvent(event); findElementByTestId('toolbar').dispatchEvent(event);
}); });
return event; return event;
} }
...@@ -115,7 +108,9 @@ describe('shared/keyboard-navigation', () => { ...@@ -115,7 +108,9 @@ describe('shared/keyboard-navigation', () => {
const currentElement = document.activeElement; const currentElement = document.activeElement;
assert.equal(currentElement.innerText, expectedItem); assert.equal(currentElement.innerText, expectedItem);
for (let element of getElements()) {
const toolbarButtons = container.querySelectorAll('a,button');
for (let element of toolbarButtons) {
if (element === currentElement) { if (element === currentElement) {
assert.equal(element.tabIndex, 0); assert.equal(element.tabIndex, 0);
} else { } else {
...@@ -202,8 +197,8 @@ describe('shared/keyboard-navigation', () => { ...@@ -202,8 +197,8 @@ describe('shared/keyboard-navigation', () => {
it('should skip hidden elements', () => { it('should skip hidden elements', () => {
renderToolbar(); renderToolbar();
getElement('bold').focus(); findElementByTestId('bold').focus();
getElement('italic').style.display = 'none'; findElementByTestId('italic').style.display = 'none';
pressKey('ArrowRight'); pressKey('ArrowRight');
...@@ -212,8 +207,8 @@ describe('shared/keyboard-navigation', () => { ...@@ -212,8 +207,8 @@ describe('shared/keyboard-navigation', () => {
it('should skip disabled elements', () => { it('should skip disabled elements', () => {
renderToolbar(); renderToolbar();
getElement('bold').focus(); findElementByTestId('bold').focus();
getElement('italic').disabled = true; findElementByTestId('italic').disabled = true;
pressKey('ArrowRight'); pressKey('ArrowRight');
...@@ -222,7 +217,7 @@ describe('shared/keyboard-navigation', () => { ...@@ -222,7 +217,7 @@ describe('shared/keyboard-navigation', () => {
it('should not respond to Up/Down arrow keys if vertical navigation is disabled', () => { it('should not respond to Up/Down arrow keys if vertical navigation is disabled', () => {
renderToolbar({ vertical: false }); renderToolbar({ vertical: false });
getElement('bold').focus(); findElementByTestId('bold').focus();
pressKey('ArrowDown'); pressKey('ArrowDown');
...@@ -231,7 +226,7 @@ describe('shared/keyboard-navigation', () => { ...@@ -231,7 +226,7 @@ describe('shared/keyboard-navigation', () => {
it('should not respond to Left/Right arrow keys if horizontal navigation is disabled', () => { it('should not respond to Left/Right arrow keys if horizontal navigation is disabled', () => {
renderToolbar({ horizontal: false }); renderToolbar({ horizontal: false });
getElement('bold').focus(); findElementByTestId('bold').focus();
pressKey('ArrowRight'); pressKey('ArrowRight');
...@@ -245,12 +240,19 @@ describe('shared/keyboard-navigation', () => { ...@@ -245,12 +240,19 @@ describe('shared/keyboard-navigation', () => {
return <div />; return <div />;
} }
// Suppress "Add @babel/plugin-transform-react-jsx-source to get a more
// detailed component stack" warning from the `render` call below.
sinon.stub(console, 'warn');
let error; let error;
try { try {
act(() => render(<BrokenToolbar />, container)); act(() => render(<BrokenToolbar />, container));
} catch (e) { } catch (e) {
error = e; error = e;
} finally {
console.warn.restore();
} }
assert.instanceOf(error, Error); assert.instanceOf(error, Error);
assert.equal(error.message, 'Container ref not set'); assert.equal(error.message, 'Container ref not set');
}); });
...@@ -259,7 +261,7 @@ describe('shared/keyboard-navigation', () => { ...@@ -259,7 +261,7 @@ describe('shared/keyboard-navigation', () => {
renderToolbar({ renderToolbar({
selector: '[data-testid=bold],[data-testid=italic]', selector: '[data-testid=bold],[data-testid=italic]',
}); });
getElement('bold').focus(); findElementByTestId('bold').focus();
pressKey('ArrowRight'); pressKey('ArrowRight');
assert.equal(currentItem(), 'Italic'); assert.equal(currentItem(), 'Italic');
...@@ -286,8 +288,8 @@ describe('shared/keyboard-navigation', () => { ...@@ -286,8 +288,8 @@ describe('shared/keyboard-navigation', () => {
it('should re-initialize tabindex attributes if current element is disabled', async () => { it('should re-initialize tabindex attributes if current element is disabled', async () => {
renderToolbar(); renderToolbar();
const boldButton = getElement('bold'); const boldButton = findElementByTestId('bold');
const italicButton = getElement('italic'); const italicButton = findElementByTestId('italic');
boldButton.focus(); boldButton.focus();
assert.equal(boldButton.tabIndex, 0); assert.equal(boldButton.tabIndex, 0);
......
...@@ -326,43 +326,37 @@ describe('MarkdownEditor', () => { ...@@ -326,43 +326,37 @@ describe('MarkdownEditor', () => {
} }
} }
context('when `isPreviewing` is false', () => { // This is a basic test that arrow key navigation is enabled.
// This is a basic test of arrow key navigation in this component. // `useArrowKeyNavigation` tests cover behavior in more detail.
// `useArrowKeyNavigation` tests cover this more fully. it('arrow keys navigate through buttons', () => {
it('arrow keys navigate through buttons', () => { // Sequence of buttons we expect to be focused when the first action
const buttons = [ // ("Bold") action is initially focused and we press the right arrow key
'Italic', // until focus returns to it.
'Quote', const buttons = [
'Insert link', 'Italic',
'Insert image', 'Quote',
'Insert math', 'Insert link',
'Numbered list', 'Insert image',
'Bulleted list', 'Insert math',
'Formatting help', 'Numbered list',
'Preview', 'Bulleted list',
'Bold', 'Formatting help',
'Italic', 'Preview',
]; 'Bold',
testArrowKeySequence(buttons); ];
}); testArrowKeySequence(buttons);
}); });
context('when `isPreviewing` is true', () => { it('arrow keys navigate through enabled buttons when `isPreviewing` is true', () => {
beforeEach(() => { const previewButton = wrapper
const previewButton = wrapper .find('button')
.find('button') .filterWhere(el => el.text() === 'Preview');
.filterWhere(el => el.text() === 'Preview'); previewButton.simulate('click');
previewButton.simulate('click');
pressKey('Home'); pressKey('Home');
});
// This is a basic test of arrow key navigation in this component. const buttons = ['Write', 'Formatting help', 'Write'];
// `useArrowKeyNavigation` tests cover this more fully. testArrowKeySequence(buttons);
it('arrow keys navigate through buttons', () => {
const buttons = ['Write', 'Formatting help', 'Write'];
testArrowKeySequence(buttons);
});
}); });
}); });
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment