Skip to content

Commit

Permalink
chore: basic decorateIcons tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kptdobe committed Sep 13, 2023
1 parent 2e82363 commit 8455c55
Show file tree
Hide file tree
Showing 7 changed files with 89 additions and 1 deletion.
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.
2 changes: 1 addition & 1 deletion web-test-runner.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@ export default {
],
},
files: [
'test/**/*.test.{html,js}',
'test/decorate/*.test.{html,js}',
],
};

0 comments on commit 8455c55

Please sign in to comment.