Skip to content

Commit

Permalink
test(post css plugin): added tests
Browse files Browse the repository at this point in the history
  • Loading branch information
sr258 committed Dec 11, 2020
1 parent 725b391 commit 42791d5
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 1 deletion.
2 changes: 1 addition & 1 deletion jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ module.exports = {
// verbose: null,

// An array of regexp patterns that are matched against all source file paths before re-running tests in watch mode
// watchPathIgnorePatterns: [],
watchPathIgnorePatterns: ['/node_modules/', '/h5p/', '/build/'],

// Whether to use watchman for file crawling
// watchman: true,
Expand Down
6 changes: 6 additions & 0 deletions src/helpers/postCssRemoveRedundantFontUrls.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,12 @@ export default function (
'embedded-opentype'
]
): Plugin {
if (!fontPreference || fontPreference.length === 0) {
throw new Error(
'You must specify the order in which fonts should be preferred as any array with at least one entry.'
);
}

return {
postcssPlugin: 'postcss-remove-redundant-font-urls',
// tslint:disable-next-line: function-name
Expand Down
29 changes: 29 additions & 0 deletions test/helpers/postCssRemoveRedundantFonts.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,26 @@ describe('postCssRemoveRedundantFonts.test', () => {
);
});

it('removes redundant fonts in the desired order', async () => {
const result = await postCss(
postCssRemoveRedundantUrls(['opentype', 'woff'])
).process(
`@font-face { src: url(path/to/font.woff) format("woff"), url(path/to/font.otf) format("opentype"); }`
);
expect(result.css).toEqual(
`@font-face { src: url(path/to/font.otf) format("opentype"); }`
);
});

it('works with quotation marks', async () => {
const result = await postCss(postCssRemoveRedundantUrls()).process(
`@font-face { src: url('path/to/font.woff') format("woff"), url("path/to/font.otf") format("opentype"); }`
);
expect(result.css).toEqual(
`@font-face { src: url('path/to/font.woff') format("woff"); }`
);
});

it('also removes redundant fonts of unknown type', async () => {
const result = await postCss(postCssRemoveRedundantUrls()).process(
`@font-face { src: url(path/to/font.woff) format("woff"), url(path/to/font.unk) format("unknown"); }`
Expand Down Expand Up @@ -47,4 +67,13 @@ describe('postCssRemoveRedundantFonts.test', () => {
`@font-face { src: url(path/to/font.otf) format("opentype"); }`
);
});

it('removes real live H5P fonts with query strings', async () => {
const result = await postCss(postCssRemoveRedundantUrls()).process(
`@font-face { src: url('fontawesome-webfont.eot?#iefix&v=4.5.0') format('embedded-opentype'),url('fontawesome-webfont.woff2?v=4.5.0') format('woff2'),url('fontawesome-webfont.woff?v=4.5.0') format('woff'),url('fontawesome-webfont.ttf?v=4.5.0') format('truetype'),url('fontawesome-webfont.svg?v=4.5.0#fontawesomeregular') format('svg'); }`
);
expect(result.css).toEqual(
`@font-face { src: url('fontawesome-webfont.woff?v=4.5.0') format('woff'); }`
);
});
});

0 comments on commit 42791d5

Please sign in to comment.