Skip to content

Commit

Permalink
Add tests for fontCssUtilities (#2641)
Browse files Browse the repository at this point in the history
  • Loading branch information
imnasnainaec authored Sep 28, 2023
1 parent 36cfdf3 commit a2dc510
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/utilities/fontCssUtilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ async function fetchText(url: string): Promise<string | undefined> {

/** Given a font and source, returns css info for the font from the source.
* If substitute is specified, sub that in as the "font-family". */
async function fetchCss(
export async function fetchCss(
font: string,
source: string,
substitute?: string
Expand Down Expand Up @@ -80,7 +80,7 @@ async function getFallbacks(
}

/** Given an array of font names, returns css info for them all. */
async function getCss(fonts: string[]) {
export async function getCss(fonts: string[]) {
// Get local css files when available.
const cssDict: Hash<string> = {};
const cssPromises = fonts.map(async (f) => {
Expand Down
72 changes: 72 additions & 0 deletions src/utilities/tests/fontCssUtilities.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import { Project } from "api";
import { newWritingSystem } from "types/writingSystem";
import { fetchCss, getCss, getProjCss } from "utilities/fontCssUtilities";

global.fetch = () =>
Promise.resolve({
blob: () => Promise.resolve({ text: mockFetchText }),
ok: true,
} as any as Response);
const mockFetchText = jest.fn();

beforeEach(() => {
jest.resetAllMocks();
});

describe("fontCssUtilities", () => {
describe("fetchCss", () => {
it("handles sources", async () => {
const css = `@font{font-family: Font}`;
mockFetchText.mockResolvedValue(css);
expect(await fetchCss("font1", "local")).toEqual(css);
expect(await fetchCss("font2", "google")).toEqual(css);
expect(await fetchCss("font3", "not-a-source")).toBeUndefined();
});

it("substitutes", async () => {
const font = "Before";
const substitute = "After";
const cssBefore = `@font{font-family: ${font}}`;
const cssAfter = `@font{font-family: ${substitute}}`;
mockFetchText.mockResolvedValueOnce(cssBefore);
expect(await fetchCss(font, "local", substitute)).toEqual(cssAfter);
});
});

describe("getCss", () => {
it("doesn't get fallbacks if css data fetched", async () => {
const css = "@started-with-at-sign{}";
mockFetchText.mockResolvedValue(css);
const cssStrings = await getCss(["font"]);
expect(cssStrings).toHaveLength(1);
expect(cssStrings).toContain(css);
expect(mockFetchText).toBeCalledTimes(1);
});

it("get fallbacks if no css data fetched", async () => {
mockFetchText.mockResolvedValueOnce(
"<html>not-started-with-at-sign</html>"
);
mockFetchText.mockResolvedValueOnce('{"google": {}}');
expect(await getCss(["font"])).toHaveLength(0);
expect(mockFetchText).toBeCalledTimes(2);
});
});

describe("getProjCss", () => {
it("gets analysis and vern fonts, handling duplicates", async () => {
mockFetchText.mockResolvedValue("@font{}");
const proj: Partial<Project> = {
analysisWritingSystems: [
newWritingSystem("bcp1a", "name1a", "font1"),
newWritingSystem("bcp2a", "name2a", "font2"),
newWritingSystem("bcp2b", "name2b", "font2"),
newWritingSystem("bcp1b", "name1b", "font1"),
newWritingSystem("bcp2c", "name2c", "font2"),
],
vernacularWritingSystem: newWritingSystem("bcp", "name", "font"),
};
expect(await getProjCss(proj as Project)).toHaveLength(3);
});
});
});

0 comments on commit a2dc510

Please sign in to comment.