Skip to content

Commit 34db3bd

Browse files
committed
feat: simplify JavaScript
- remove unused navigation PHP logic - remove unused/unuseful JS - change vague init script to named function - rename functions to helpers - use :focus-visible instead of safe-focus JS - move class helper methods into one script - rewrite disclosure/aria logic - write tests for remaining JS - limit coverage checks to src/js - improve coverage
1 parent a794a99 commit 34db3bd

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+316
-903
lines changed

docs/wordpress.md

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,17 @@
1-
WordPress
2-
=========
1+
# WordPress
32

4-
Upgrading Process
5-
-----------------
3+
## Upgrading Process
64

75
1. Update the WordPress install to the latest version.
86
1. If you can confirm the update made no breaking changes:
9-
1. Change `WP_VERSION` in `Dockerfile` and `.circleci/config.yml` to the latest version.
10-
1. Submit a PR with the update.
7+
1. Change `WP_VERSION` in `Dockerfile` and `.circleci/config.yml` to the latest version.
8+
1. Submit a PR with the update.
119
1. If the update made breaking changes. Create a bug card. Do not push any changes.
1210

13-
Suggested Settings
14-
------------------
11+
## Suggested Settings
1512

1613
- **Permalinks**: Most likely, you want your urls to look like `example.com/name-of-page` rather than `example.com/?page_id=2`. To accomplish this:
1714

18-
1. Go to `Settings` > `Permalinks` in the admin dashboard
19-
1. Select "Post Name" for the permalink setting
20-
1. Hit "Save Changes"
21-
22-
- **Menus**: There has been a "Primary" menu registered and displayed in the Header. It utilizes a Custom Navigation Walker found in `src/php/inc/class-sparkpress-walker.php`. To control this menu:
23-
24-
1. Visit `Appearance` > `Menus`
25-
1. Create a Menu with the "Display Location" set to "Primary"
15+
1. Go to `Settings` > `Permalinks` in the admin dashboard
16+
1. Select "Post Name" for the permalink setting
17+
1. Hit "Save Changes"

src/js/functions/add-class.js

Lines changed: 0 additions & 3 deletions
This file was deleted.

src/js/functions/add-class.test.js

Lines changed: 0 additions & 22 deletions
This file was deleted.

src/js/functions/body-fixed.js

Lines changed: 0 additions & 7 deletions
This file was deleted.

src/js/functions/remove-class.js

Lines changed: 0 additions & 3 deletions
This file was deleted.

src/js/functions/toggle-aria.js

Lines changed: 0 additions & 9 deletions
This file was deleted.

src/js/functions/toggle-state.js

Lines changed: 0 additions & 13 deletions
This file was deleted.
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
import '@testing-library/jest-dom/vitest';
2+
import { describe, beforeEach, expect, it } from 'vitest';
3+
import { addClass, removeClass, toggleClass } from '../toggle-class';
4+
5+
describe('addClass', () => {
6+
beforeEach(() => {
7+
document.body.innerHTML = `<div id="test-element"></div>`;
8+
});
9+
10+
it('adds the class to the specified element', () => {
11+
const element = document.querySelector('#test-element');
12+
addClass(element, 'test-class');
13+
14+
expect(element).toHaveAttribute('class', 'test-class');
15+
});
16+
17+
it('does not throw an error when the element does not exist', () => {
18+
const element = document.querySelector('#nonexistent-element');
19+
20+
expect(() => addClass(element, 'test-class')).not.toThrowError();
21+
});
22+
});
23+
24+
describe('removeClass', () => {
25+
beforeEach(() => {
26+
document.body.innerHTML = `<div id="test-element" class="test-class some-other-class"></div>`;
27+
});
28+
29+
it('removes the class from the specified element', () => {
30+
const element = document.querySelector('#test-element');
31+
removeClass(element, 'test-class');
32+
33+
expect(element).not.toHaveAttribute('class', 'test-class');
34+
expect(element).toHaveAttribute('class', 'some-other-class');
35+
});
36+
37+
it('does not throw an error when the element does not exist', () => {
38+
const element = document.querySelector('#nonexistent-element');
39+
40+
expect(() => removeClass(element, 'test-class')).not.toThrowError();
41+
});
42+
});
43+
44+
describe('toggleClass', () => {
45+
beforeEach(() => {
46+
document.body.innerHTML = `<div id="test-element" class="test-class some-other-class"></div>`;
47+
});
48+
49+
it('toggles the class on the specified element', () => {
50+
const element = document.querySelector('#test-element');
51+
toggleClass(element, 'test-class');
52+
53+
expect(element).toHaveAttribute('class', 'some-other-class');
54+
55+
toggleClass(element, 'test-class');
56+
57+
expect(element).toHaveAttribute('class', 'some-other-class test-class');
58+
59+
toggleClass(element, 'some-other-class');
60+
61+
expect(element).toHaveAttribute('class', 'test-class');
62+
});
63+
64+
it('does not throw an error when the element does not exist', () => {
65+
const element = document.querySelector('#nonexistent-element');
66+
67+
expect(() => toggleClass(element, 'test-class')).not.toThrowError();
68+
});
69+
});
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
import '@testing-library/jest-dom/vitest';
2+
import { screen } from '@testing-library/dom';
3+
import { describe, beforeEach, expect, it } from 'vitest';
4+
import { initializeDisclosureButtonHandler } from '../toggle-disclosure';
5+
6+
describe('initializeDisclosureButtonHandler', () => {
7+
beforeEach(() => {
8+
document.body.innerHTML = `
9+
<button class="cmp-disclosure-button" aria-expanded="false" aria-controls="test-element-closed">Reveal Content</button>
10+
<p id="test-element-closed" hidden>This content will be revealed!</p>
11+
<button class="cmp-disclosure-button" aria-expanded="true" aria-controls="test-element-open">Hide Content</button>
12+
<p id="test-element-open">This content will be hidden!</p>
13+
<button class="cmp-disclosure-button" aria-expanded="false">Do Nothing</button>
14+
<p id="test-element-forever-hidden" hidden>This content will always be hidden!</p>
15+
`;
16+
});
17+
18+
it('opens the first menu when the button is clicked', () => {
19+
initializeDisclosureButtonHandler('.cmp-disclosure-button');
20+
21+
const revealContentButton = screen.getByText('Reveal Content');
22+
revealContentButton.click();
23+
expect(revealContentButton).toHaveAttribute('aria-expanded', 'true');
24+
expect(screen.getByText('This content will be revealed!')).not.toHaveAttribute('hidden');
25+
});
26+
27+
it('closes the second menu when the button is clicked', () => {
28+
initializeDisclosureButtonHandler('.cmp-disclosure-button');
29+
30+
const hideContentButton = screen.getByText('Hide Content');
31+
hideContentButton.click();
32+
expect(hideContentButton).toHaveAttribute('aria-expanded', 'false');
33+
expect(screen.getByText('This content will be hidden!')).toHaveAttribute('hidden');
34+
});
35+
36+
it('toggles the menu on subsequent clicks', () => {
37+
initializeDisclosureButtonHandler('.cmp-disclosure-button');
38+
39+
const revealContentButton = screen.getByText('Reveal Content');
40+
revealContentButton.click();
41+
expect(revealContentButton).toHaveAttribute('aria-expanded', 'true');
42+
expect(screen.getByText('This content will be revealed!')).not.toHaveAttribute('hidden');
43+
44+
revealContentButton.click();
45+
expect(revealContentButton).toHaveAttribute('aria-expanded', 'false');
46+
expect(screen.getByText('This content will be revealed!')).toHaveAttribute('hidden');
47+
48+
revealContentButton.click();
49+
expect(revealContentButton).toHaveAttribute('aria-expanded', 'true');
50+
expect(screen.getByText('This content will be revealed!')).not.toHaveAttribute('hidden');
51+
52+
revealContentButton.click();
53+
expect(revealContentButton).toHaveAttribute('aria-expanded', 'false');
54+
expect(screen.getByText('This content will be revealed!')).toHaveAttribute('hidden');
55+
});
56+
57+
it("does nothing when the button isn't pointing to content correctly", () => {
58+
initializeDisclosureButtonHandler('.cmp-disclosure-button');
59+
60+
const uselessButton = screen.getByText('Do Nothing');
61+
uselessButton.click();
62+
expect(uselessButton).toHaveAttribute('aria-expanded', 'false');
63+
expect(screen.getByText('This content will always be hidden!')).toHaveAttribute('hidden');
64+
});
65+
});
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import '@testing-library/jest-dom/vitest';
2+
import { describe, beforeEach, expect, it } from 'vitest';
3+
import { toggleNoJS } from '../toggle-no-js';
4+
5+
describe('toggleNoJS', () => {
6+
beforeEach(() => {
7+
document.documentElement.setAttribute('class', 'no-js');
8+
});
9+
10+
it("replaces no-js with js in html element's class", () => {
11+
const htmlElement = document.querySelector('html');
12+
expect(htmlElement).toHaveAttribute('class', 'no-js');
13+
14+
toggleNoJS();
15+
16+
expect(htmlElement).toHaveAttribute('class', 'js');
17+
});
18+
});

0 commit comments

Comments
 (0)