Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: basic decorateIcons tests #9

Merged
merged 2 commits into from
Sep 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/decorate.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ export async function decorateIcons(element) {
};
}
} catch (error) {
/* c8 ignore next 4 */
ICONS_CACHE[iconName] = false;
// eslint-disable-next-line no-console
console.error(error);
Expand Down
39 changes: 39 additions & 0 deletions test/decorate/decorateIcons.basic.test.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<!DOCTYPE html>
<html lang="en">
<body>
<div>
<span class="icon icon-a"></span>
<p><span class="icon icon-b"></span></p>
<a href="#"><span class="icon icon-a"></span></a>
<p><span class="icon icon-doesnotexist"></span></p>
</div>
<script type="module">
/* eslint-env mocha */
import { runTests } from '@web/test-runner-mocha';
import { expect } from '@esm-bundle/chai';
import { decorateIcons } from '../../src/decorate.js';

window.hlx = {};

runTests(() => {
it('decorates icons', async () => {
window.hlx.codeBasePath = '/test/fixtures';

await decorateIcons(document.body);

const icons = document.querySelectorAll('.icon');
expect(icons.length).to.equal(4);
icons.forEach((icon) => {
expect(icon.childElementCount).to.be.equal(1);
expect(icon.firstElementChild.tagName).to.be.equal('svg');
});

const sprite = document.getElementById('franklin-svg-sprite');
expect(sprite).to.not.be.null;
expect(sprite.tagName).to.be.equal('svg');
expect(sprite.childElementCount).to.be.equal(2);
});
});
</script>
</body>
</html>
37 changes: 37 additions & 0 deletions test/decorate/decorateIcons.styled.test.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<!DOCTYPE html>
<html lang="en">
<body>
<div>
<span class="icon icon-styled"></span>
</div>
<script type="module">
/* eslint-env mocha */
import { runTests } from '@web/test-runner-mocha';
import { expect } from '@esm-bundle/chai';
import { decorateIcons } from '../../src/decorate.js';

window.hlx = {};

runTests(() => {
it('decorates styled icons', async () => {
window.hlx.codeBasePath = '/test/fixtures';

await decorateIcons(document.body);

const icons = document.querySelectorAll('.icon');
expect(icons.length).to.equal(1);
const icon = icons[0];
expect(icon.childElementCount).to.be.equal(1);
expect(icon.firstElementChild.tagName).to.be.equal('svg');
expect(icon.querySelector('.someclass')).to.not.be.null;

// sprite should be empty
const sprite = document.getElementById('franklin-svg-sprite');
expect(sprite).to.not.be.null;
expect(sprite.tagName).to.be.equal('svg');
expect(sprite.childElementCount).to.be.equal(0);
});
});
</script>
</body>
</html>
1 change: 1 addition & 0 deletions test/fixtures/icons/a.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions test/fixtures/icons/b.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
9 changes: 9 additions & 0 deletions test/fixtures/icons/styled.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading