-
Notifications
You must be signed in to change notification settings - Fork 2
Merge pull request #6 from compositive/improve-test-suite #6
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
Merged
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
2ee7995
Fix vscode-jest configuration
lordofthelake 264e546
Wrap tests in describe() blocks
lordofthelake 12f975c
Add @types/jest to dev dependencies
lordofthelake 6f605ac
Add tests for findFontName()
lordofthelake 97c7290
Add tests for makeImageDataFromURL
lordofthelake 9fe45d7
Add skipped test for buggy behaviour
lordofthelake cce4278
Add better comment to findFontName()'s test
lordofthelake 9a07e45
Add tests for createStringMeasurer()
lordofthelake ed6617e
More @types/jest to be a devDependency
lordofthelake 92ec7a5
Remove leftovers of the fixtures generation
lordofthelake 97e294a
Trigger GitHub actions
lordofthelake 32d5955
Remove or rewrite failing tests
lordofthelake 520390e
Adjust RegExp
lordofthelake File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,5 +2,6 @@ | |
"files.associations": { | ||
"stdio.h": "c", | ||
"node_api.h": "c" | ||
} | ||
} | ||
}, | ||
"jest.pathToJest": "npm test --" | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,104 @@ | ||
const { createStringMeasurer } = require("../"); | ||
|
||
test("measures an empty string", () => { | ||
const size = createStringMeasurer([], 400); | ||
const testBody = ({ content, within, expectedWidth, expectedHeight, ...textStyles }) => { | ||
const { width, height } = createStringMeasurer([{ content, textStyles }], within); | ||
|
||
expect(size).toEqual({ | ||
height: 15, | ||
width: 0 | ||
// TODO(lordofthelake): Sketch uses round numbers for widths. | ||
// Considering that these are pixels, would it make sense to return rounded numbers too? | ||
const roundedUpWidth = Math.ceil(width); | ||
|
||
expect(roundedUpWidth).toBe(expectedWidth); | ||
expect(height).toBe(expectedHeight); | ||
}; | ||
|
||
const testDescription = (...parameters) => { | ||
let textStyles = parameters.includes("fontFamily") ? "$fontFamily" : "[System font]"; | ||
if (parameters.includes("fontWeight")) textStyles += " $fontWeight"; | ||
if (parameters.includes("fontStyle")) textStyles += " $fontStyle"; | ||
if (parameters.includes("fontSize")) textStyles += " $fontSize\u200bpx"; | ||
if (parameters.includes("lineHeight")) textStyles += "/$lineHeight\u200bpx"; | ||
if (parameters.includes("letterSpacing")) textStyles += " [$letterSpacing\u200bpx letter spacing]"; | ||
if (parameters.includes("textAlign")) textStyles += ", $textAlign-aligned,"; | ||
|
||
return `within $within\u200bpx, measures \`$content\` in ${textStyles} to be $expectedWidth✕$expectedHeight\u200bpx`; | ||
}; | ||
|
||
describe("createStringMeasurer()", () => { | ||
it("measures an empty string", () => { | ||
const size = createStringMeasurer([], 400); | ||
|
||
expect(size).toEqual({ | ||
height: 15, | ||
width: 0 | ||
}); | ||
}); | ||
|
||
describe("when setting a line height", () => { | ||
test.each` | ||
content | fontFamily | fontSize | lineHeight | within | expectedWidth | expectedHeight | ||
${"Hello"} | ${"Helvetica"} | ${14} | ${16} | ${400} | ${32} | ${16} | ||
${"Hello"} | ${"Helvetica"} | ${24} | ${26} | ${400} | ${55} | ${26} | ||
${"Hello"} | ${"Helvetica"} | ${72} | ${74} | ${400} | ${165} | ${74} | ||
${"Hello"} | ${"Helvetica"} | ${120} | ${122} | ${400} | ${274} | ${122} | ||
${"Hello"} | ${"Impact"} | ${24} | ${26} | ${400} | ${52} | ${26} | ||
${"Hello"} | ${"Helvetica Neue"} | ${24} | ${26} | ${400} | ${55} | ${26} | ||
${"Hello"} | ${"Georgia"} | ${24} | ${26} | ${400} | ${58} | ${26} | ||
`(testDescription("fontFamily", "fontSize", "lineHeight"), testBody); | ||
}); | ||
|
||
describe("when spaces are present", () => { | ||
test.each` | ||
content | fontFamily | fontSize | lineHeight | within | expectedWidth | expectedHeight | ||
${"."} | ${"Helvetica"} | ${24} | ${28} | ${400} | ${7} | ${28} | ||
${".."} | ${"Helvetica"} | ${24} | ${28} | ${400} | ${14} | ${28} | ||
${". ."} | ${"Helvetica"} | ${24} | ${28} | ${400} | ${21} | ${28} | ||
${". ."} | ${"Helvetica"} | ${24} | ${28} | ${400} | ${27} | ${28} | ||
`(testDescription("fontFamily", "fontSize", "lineHeight"), testBody); | ||
}); | ||
|
||
describe("when the text width exceeds the available space", () => { | ||
test.each` | ||
content | fontFamily | fontSize | lineHeight | within | expectedWidth | expectedHeight | ||
${"Hello"} | ${"Helvetica"} | ${24} | ${29} | ${100} | ${55} | ${29} | ||
${"Hello Hello"} | ${"Helvetica"} | ${24} | ${29} | ${200} | ${117} | ${29} | ||
${"Hello Hello"} | ${"Helvetica"} | ${24} | ${29} | ${100} | ${62} | ${58} | ||
${"HelloHello"} | ${"Helvetica"} | ${24} | ${29} | ${100} | ${97} | ${58} | ||
`(testDescription("fontFamily", "fontSize", "lineHeight"), testBody); | ||
}); | ||
|
||
describe("when using different font weights and styles", () => { | ||
test.each` | ||
content | fontFamily | fontSize | lineHeight | within | fontWeight | fontStyle | expectedWidth | expectedHeight | ||
${"Hello"} | ${"Helvetica"} | ${24} | ${29} | ${100} | ${"normal"} | ${"normal"} | ${55} | ${29} | ||
${"Hello"} | ${"Helvetica"} | ${24} | ${29} | ${100} | ${"normal"} | ${"italic"} | ${55} | ${29} | ||
${"Hello"} | ${"Helvetica"} | ${24} | ${29} | ${100} | ${"bold"} | ${"normal"} | ${59} | ${29} | ||
${"Hello"} | ${"Helvetica"} | ${24} | ${29} | ${100} | ${"bold"} | ${"italic"} | ${59} | ${29} | ||
${"Hello"} | ${"Helvetica"} | ${24} | ${29} | ${100} | ${"300"} | ${"normal"} | ${55} | ${29} | ||
${"Hello"} | ${"Helvetica"} | ${24} | ${29} | ${100} | ${"300"} | ${"italic"} | ${55} | ${29} | ||
`(testDescription("fontFamily", "fontSize", "fontWeight", "fontStyle", "lineHeight"), testBody); | ||
}); | ||
|
||
describe("when using different alignments", () => { | ||
const loremIpsum = | ||
"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua."; | ||
|
||
test.each` | ||
content | fontFamily | fontSize | lineHeight | textAlign | within | expectedWidth | expectedHeight | ||
${loremIpsum} | ${"Helvetica"} | ${24} | ${29} | ${"left"} | ${200} | ${200} | ${232} | ||
${loremIpsum} | ${"Helvetica"} | ${24} | ${29} | ${"right"} | ${200} | ${200} | ${232} | ||
${loremIpsum} | ${"Helvetica"} | ${24} | ${29} | ${"center"} | ${200} | ${200} | ${232} | ||
${loremIpsum} | ${"Helvetica"} | ${24} | ${29} | ${"justify"} | ${200} | ${200} | ${232} | ||
`(testDescription("fontFamily", "fontSize", "textAlign", "lineHeight"), testBody); | ||
}); | ||
|
||
describe("when using letter spacing", () => { | ||
test.each` | ||
content | fontFamily | fontSize | lineHeight | letterSpacing | within | expectedWidth | expectedHeight | ||
${"Hello"} | ${"Helvetica"} | ${24} | ${29} | ${-1} | ${200} | ${50} | ${29} | ||
${"Hello"} | ${"Helvetica"} | ${24} | ${29} | ${0} | ${200} | ${55} | ${29} | ||
${"Hello"} | ${"Helvetica"} | ${24} | ${29} | ${1} | ${200} | ${60} | ${29} | ||
${"Hello"} | ${"Helvetica"} | ${24} | ${29} | ${10} | ${200} | ${105} | ${29} | ||
${"Hello"} | ${"Helvetica"} | ${24} | ${29} | ${20} | ${200} | ${155} | ${29} | ||
`(testDescription("fontFamily", "fontSize", "letterSpacing", "lineHeight"), testBody); | ||
}); | ||
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,82 @@ | ||
const { findFontName } = require("../"); | ||
|
||
test("returns font name", () => { | ||
const fontName = findFontName({ | ||
fontFamily: "Impact", | ||
fontSize: 18 | ||
const testEach = cases => { | ||
test.each(cases)("for %p returns %s", (textStyle, expectedFontName) => { | ||
if (expectedFontName instanceof RegExp) expect(findFontName(textStyle)).toMatch(expectedFontName); | ||
else expect(findFontName(textStyle)).toEqual(expectedFontName); | ||
}); | ||
}; | ||
|
||
expect(fontName).toEqual("Impact"); | ||
describe("findFontName()", () => { | ||
describe("with regular fonts", () => { | ||
testEach([ | ||
// Regular font, one style available | ||
[{ fontFamily: "Impact" }, "Impact"], | ||
[{ fontFamily: "Impact", fonstStyle: "italic" }, "Impact"], | ||
|
||
// Font with more variants | ||
[{ fontFamily: "Helvetica", fontWeight: "bold" }, "Helvetica-Bold"], | ||
[{ fontFamily: "Helvetica", fontStyle: "italic" }, "Helvetica-Oblique"], | ||
[{ fontFamily: "Helvetica", fontStyle: "oblique" }, "Helvetica-Oblique"], | ||
[{ fontFamily: "Helvetica", fontWeight: "300" }, "Helvetica-Light"], | ||
[{ fontFamily: "Helvetica", fontWeight: "300", fontStyle: "italic" }, "Helvetica-LightOblique"] | ||
]); | ||
|
||
describe("when a weight is missing, it uses the next one available", () => { | ||
testEach([ | ||
[{ fontFamily: "Helvetica", fontWeight: "900" }, "Helvetica-Bold"], | ||
[{ fontFamily: "Helvetica", fontWeight: "600" }, "Helvetica-Bold"], | ||
[{ fontFamily: "Helvetica", fontWeight: "500" }, "Helvetica-Bold"], | ||
[{ fontFamily: "Helvetica", fontWeight: "200" }, "Helvetica-Light"] | ||
]); | ||
}); | ||
}); | ||
|
||
describe("for a system font, uses SF Text", () => { | ||
testEach([ | ||
[{ fontFamily: ".AppleSystemUIFont" }, /^\.SFNS\w*(-Regular)?$/], | ||
[{ fontFamily: "System" }, /^\.SFNS/], | ||
[{ fontFamily: "System", fontSize: 12 }, /^\.SFNS\w*(-Regular)?$/], | ||
[{ fontFamily: "System", fontWeight: "bold" }, /^\.SFNS\w*-Bold$/], | ||
[{ fontFamily: "System", fontStyle: "italic" }, /^\.SFNS\w*-(Regular)?Italic$/], | ||
[{ fontFamily: "System", fontWeight: "bold", fontStyle: "italic" }, /^\.SFNS\w*-BoldItalic$/], | ||
[{ fontFamily: "System", fontStyle: "oblique" }, /^\.SFNS\w*-(Regular)?Italic$/] | ||
]); | ||
}); | ||
|
||
describe("missing fonts default to the system font", () => { | ||
testEach([[{ fontFamily: "MissingFont" }, /^\.SFNS\w*(-Regular)?$/]]); | ||
}); | ||
|
||
describe("when the fontFamily property is missing", () => { | ||
// TODO(lordofthelake): Does this make sense, considering the missing font case? | ||
// Shouldn't it default to the system font? | ||
it("defaults to Helvetica", () => { | ||
expect(findFontName({})).toEqual("Helvetica"); | ||
}); | ||
}); | ||
|
||
describe("when the fontFamily property is blank", () => { | ||
// TODO(lordofthelake): Coherence problem as above. | ||
it("defaults to the system font", () => { | ||
expect(findFontName({ fontFamily: "" })).toMatch(/^\.SFNS\w*(-Regular)?$/); | ||
}); | ||
}); | ||
|
||
describe("when the fontWeight is invalid", () => { | ||
it("when given a number, throws an error", () => { | ||
// TODO(lordofthelake): Shouldn't this case be handled more gracefully? | ||
expect(() => findFontName({ fontWeight: 300 })).toThrow("Could not get string length"); | ||
}); | ||
|
||
it("when given an invalid string, defaults to regular", () => { | ||
expect(findFontName({ fontFamily: "Helvetica", fontWeight: "bolder" })).toEqual("Helvetica"); | ||
}); | ||
}); | ||
|
||
describe("when the fontStyle is invalid", () => { | ||
it("returns the regular version", () => { | ||
expect(findFontName({ fontFamily: "Helvetica", fontStyle: "happy" })).toEqual("Helvetica"); | ||
}); | ||
}); | ||
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,24 @@ | ||
const { makeImageDataFromURL } = require("../"); | ||
|
||
test("returns a default image", () => { | ||
const data = makeImageDataFromURL(); | ||
const itEncodesAsPNG = (description, ...args) => { | ||
it(description, async () => { | ||
const data = makeImageDataFromURL(...args); | ||
|
||
expect(typeof data).toEqual("string"); | ||
}); | ||
expect(typeof data).toEqual("string"); | ||
expect(Buffer.from(data, "base64")[0]).toBe(0x89); // PNG byte mark | ||
}); | ||
}; | ||
|
||
describe("makeImageDataFromURL()", () => { | ||
itEncodesAsPNG("returns a base64-encoded red image"); | ||
|
||
test("returns a fetched image", () => { | ||
const data = makeImageDataFromURL( | ||
itEncodesAsPNG( | ||
"returns the fetched image as a base64-encoded string", | ||
"https://www.mjt.me.uk/assets/images/smallest-png/openstreetmap.png" | ||
); | ||
|
||
expect(typeof data).toEqual("string"); | ||
itEncodesAsPNG( | ||
"re-encodes images in other formats to PNG", | ||
"https://upload.wikimedia.org/wikipedia/commons/3/38/JPEG_example_JPG_RIP_001.jpg" | ||
); | ||
}); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.