Skip to content

Commit

Permalink
test: add tests for font.css.template changes
Browse files Browse the repository at this point in the history
  • Loading branch information
LewisDaleUK committed Dec 1, 2024
1 parent ae4b8d5 commit 410e710
Show file tree
Hide file tree
Showing 3 changed files with 126 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/two-islands-dance.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'style-dictionary': patch
---

Fix font-style and font-weight logic for fonts.css.template.js
119 changes: 119 additions & 0 deletions __tests__/common/templates/css/fonts.css.template.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
import { expect } from 'chai';
import cssFontsTemplate from '../../../../lib/common/templates/css/fonts.css.template.js';

describe('common', () => {
describe('templates', () => {
describe('css', () => {
describe('fonts.css.template', () => {
it('should produce a valid css font-face declaration without weight or style defined', () => {
const tokens = {
asset: {
font: {
myFont: {
name: {
value: 'font',
type: 'fontFamily',
},
ttf: {
value: 'font.ttf',
type: 'asset',
},
},
},
},
};
const output = cssFontsTemplate(tokens);
expect(output).to.equal(`@font-face {
font-family: "font";
src: url('../font.ttf') format('truetype');
}`);
});
it('should produce a valid css font-face declaration with a weight defined', () => {
const tokens = {
asset: {
font: {
myFont: {
name: {
value: 'font',
type: 'fontFamily',
},
ttf: {
value: 'font.ttf',
type: 'asset',
},
weight: {
value: 400,
},
},
},
},
};
const output = cssFontsTemplate(tokens);
expect(output).to.equal(`@font-face {
font-family: "font";
src: url('../font.ttf') format('truetype');
font-weight: 400;
}`);
});
it('should produce a valid css font-face declaration with a style defined', () => {
const tokens = {
asset: {
font: {
myFont: {
name: {
value: 'font',
type: 'fontFamily',
},
ttf: {
value: 'font.ttf',
type: 'asset',
},
style: {
value: 'normal',
},
},
},
},
};
const output = cssFontsTemplate(tokens);
expect(output).to.equal(`@font-face {
font-family: "font";
src: url('../font.ttf') format('truetype');
font-style: normal;
}`);
});
it('should produce a valid css font-face declaration with both style and weight defined', () => {
const tokens = {
asset: {
font: {
myFont: {
name: {
value: 'font',
type: 'fontFamily',
},
ttf: {
value: 'font.ttf',
type: 'asset',
},
style: {
value: 'normal',
},
weight: {
value: 400,
},
},
},
},
};
const output = cssFontsTemplate(tokens);
expect(output).to.equal(`@font-face {
font-family: "font";
src: url('../font.ttf') format('truetype');
font-style: normal;
font-weight: 400;
}`);
});
});
});
});
});
4 changes: 2 additions & 2 deletions lib/common/templates/css/fonts.css.template.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export default (tokens) =>
return `@font-face {
font-family: "${font.name.value}";
src: ${fileFormatArr.join(',\n\t\t')};
${font.style ? `\n font-style: ${font.style.value};` : ''}${
font.weight ? `\n font-weight: ${font.weight.value};` : ''
${font.style ? ` font-style: ${font.style.value};\n` : ''}${
font.weight ? ` font-weight: ${font.weight.value};\n` : ''
}}`;
})}`;

0 comments on commit 410e710

Please sign in to comment.