Skip to content

Commit

Permalink
update tests to use inline snapshots
Browse files Browse the repository at this point in the history
  • Loading branch information
wollardj committed Mar 31, 2022
1 parent 670e22f commit 26d93f4
Show file tree
Hide file tree
Showing 9 changed files with 120 additions and 55 deletions.
6 changes: 2 additions & 4 deletions __tests__/catch-invalid-plist.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
import plist from "../src/index";

it("Throws an error on improperly formatted plist", () => {
function doIt() {
return plist.readFileSync(`${__dirname}/test-xml1-invalid.plist`);
}
const doIt = () => plist.readFileSync(`${__dirname}/test-xml1-invalid.plist`);
expect(doIt).toThrow();
});

it("returns an empty object when the file is zero bytes", () => {
const obj = plist.readFileSync(`${__dirname}/test-xml1-invalid-2.plist`);
expect(obj).toEqual({});
expect(obj).toMatchInlineSnapshot(`Object {}`);
});
15 changes: 13 additions & 2 deletions __tests__/parse.test.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,22 @@
import plist from "../src/index";
import { DemoFile } from "./utils/types";

describe("String parsing", () => {
it("can parse a string", () => {
const doc = plist.readFileSync(`${__dirname}/test-binary1.plist`);
const doc = plist.readFileSync<DemoFile>(`${__dirname}/test-binary1.plist`);
const plistString = plist.stringify(doc);
const parsedDoc = plist.parse(plistString);

return expect(parsedDoc).toEqual(doc);
return expect(parsedDoc).toMatchInlineSnapshot(`
Object {
"Birth Year": 1942,
"Name": "John Doe",
"Travel Log": Array [
"Tokyo, Honshu, Japan",
"Philadelphia, PA",
"Recife, Pernambuco, Brazil",
],
}
`);
});
});
33 changes: 22 additions & 11 deletions __tests__/readFile-binary1.test.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,37 @@
import plist from "../src/index";

const filePath = `${__dirname}/test-binary1.plist`;
const reference = {
"Travel Log": [
"Tokyo, Honshu, Japan",
"Philadelphia, PA",
"Recife, Pernambuco, Brazil",
],
"Birth Year": 1942,
Name: "John Doe",
};

describe("readFileSync can properly load and read a binary file", () => {
it("has the proper values", () => {
expect(plist.readFileSync(filePath)).toMatchObject(reference);
expect(plist.readFileSync(filePath)).toMatchInlineSnapshot(`
Object {
"Birth Year": 1942,
"Name": "John Doe",
"Travel Log": Array [
"Tokyo, Honshu, Japan",
"Philadelphia, PA",
"Recife, Pernambuco, Brazil",
],
}
`);
});
});

describe("readFile works asynchronously", () => {
it("has the proper values", (done) => {
plist.readFile(filePath, (error, contents) => {
expect(contents).toMatchObject(reference);
expect(contents).toMatchInlineSnapshot(`
Object {
"Birth Year": 1942,
"Name": "John Doe",
"Travel Log": Array [
"Tokyo, Honshu, Japan",
"Philadelphia, PA",
"Recife, Pernambuco, Brazil",
],
}
`);
done();
});
});
Expand Down
48 changes: 26 additions & 22 deletions __tests__/readFile-xml1.test.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,28 @@
import plist from "../src/index";
import { DemoFile } from "./utils/types";

const filePath = `${__dirname}/test-xml1.plist`;

type TestXml1 = {
"Birth Year": number;
Name: string;
"Empty String": string;
"Travel Log": string[];
};
type TestXml1 = DemoFile & { "Empty String": string };

describe("readFileSync can properly load and read a file", () => {
const contents = plist.readFileSync<TestXml1>(filePath);
it("has the proper values", () => {
if (!contents["Name"]) {
fail(`Failed to parse ${filePath}`);
}
expect(contents.Name).toBe("John Doe");
expect(contents["Birth Year"]).toBe(1942);
expect(contents["Empty String"]).toBe("");
expect(contents["Travel Log"]).toEqual([
"Tokyo, Honshu, Japan",
"Philadelphia, PA",
"Recife, Pernambuco, Brazil",
]);
expect(contents).toMatchInlineSnapshot(`
Object {
"Birth Year": 1942,
"Empty String": "",
"Name": "John Doe",
"Travel Log": Array [
"Tokyo, Honshu, Japan",
"Philadelphia, PA",
"Recife, Pernambuco, Brazil",
],
}
`);
});
});

Expand All @@ -32,14 +32,18 @@ describe("readFile works asynchronously", () => {
if (!contents) {
fail(`Failed to parse ${filePath}`);
}
expect(contents.Name).toBe("John Doe");
expect(contents["Birth Year"]).toBe(1942);
expect(contents["Empty String"]).toBe("");
expect(contents["Travel Log"]).toEqual([
"Tokyo, Honshu, Japan",
"Philadelphia, PA",
"Recife, Pernambuco, Brazil",
]);
expect(contents).toMatchInlineSnapshot(`
Object {
"Birth Year": 1942,
"Empty String": "",
"Name": "John Doe",
"Travel Log": Array [
"Tokyo, Honshu, Japan",
"Philadelphia, PA",
"Recife, Pernambuco, Brazil",
],
}
`);
done();
});
});
Expand Down
5 changes: 5 additions & 0 deletions __tests__/utils/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export type DemoFile = {
"Birth Year": number;
Name: string;
"Travel Log": string[];
};
24 changes: 22 additions & 2 deletions __tests__/writeFile-binary1.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,17 @@ describe("writeBinaryFileSync can properly load and read a file", () => {
it("has the proper values", (done) => {
plist.writeBinaryFileSync(filePath, testObj);
plist.readFile(filePath, (error, contents) => {
expect(contents).toMatchObject(testObj);
expect(contents).toMatchInlineSnapshot(`
Object {
"Birth Year": 1942,
"Name": "John Doe",
"Travel Log": Array [
"Tokyo, Honshu, Japan",
"Philadelphia, PA",
"Recife, Pernambuco, Brazil",
],
}
`);
done();
});
});
Expand All @@ -25,7 +35,17 @@ describe("writeBinaryFile works asynchronously", () => {
it("has the proper values", (done) => {
plist.writeBinaryFile(filePath, testObj, () => {
plist.readFile(filePath, (error, contents) => {
expect(contents).toMatchObject(testObj);
expect(contents).toMatchInlineSnapshot(`
Object {
"Birth Year": 1942,
"Name": "John Doe",
"Travel Log": Array [
"Tokyo, Honshu, Japan",
"Philadelphia, PA",
"Recife, Pernambuco, Brazil",
],
}
`);
done();
});
});
Expand Down
24 changes: 22 additions & 2 deletions __tests__/writeFile-xml1.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,17 @@ describe("writeFileSync can produce a valid file", () => {
it("has the proper values", (done) => {
plist.writeFileSync(filePath, testObj);
plist.readFile(filePath, (error, contents) => {
expect(contents).toMatchObject(testObj);
expect(contents).toMatchInlineSnapshot(`
Object {
"Birth Year": 1942,
"Name": "John Doe",
"Travel Log": Array [
"Tokyo, Honshu, Japan",
"Philadelphia, PA",
"Recife, Pernambuco, Brazil",
],
}
`);
done();
});
});
Expand All @@ -25,7 +35,17 @@ describe("writeFile works asynchronously", () => {
it("has the proper values", (done) => {
plist.writeFile(filePath, testObj, () => {
plist.readFile(filePath, (error, contents) => {
expect(contents).toMatchObject(testObj);
expect(contents).toMatchInlineSnapshot(`
Object {
"Birth Year": 1942,
"Name": "John Doe",
"Travel Log": Array [
"Tokyo, Honshu, Japan",
"Philadelphia, PA",
"Recife, Pernambuco, Brazil",
],
}
`);
done();
});
});
Expand Down
1 change: 1 addition & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@
module.exports = {
preset: "ts-jest",
testEnvironment: "node",
modulePathIgnorePatterns: ["src", "__tests__/utils"],
};
19 changes: 7 additions & 12 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,7 @@ import { writeFile } from "./writeFile";
import { writeFileSync } from "./writeFileSync";

// "modern" named exports
export { parse } from "./parse";
export { readFile } from "./readFile";
export { readFileSync } from "./readFileSync";
export { stringify } from "./stringify";
export type { callbackFn, PlistJsObj, StringOrBuffer } from "./types";
export { writeBinaryFile } from "./writeBinaryFile";
export { writeBinaryFileSync } from "./writeBinaryFileSync";
export { writeFile } from "./writeFile";
export { writeFileSync } from "./writeFileSync";

// preserve backwards compatibility
module.exports = {
const SimplePlist = {
bplistCreator,
bplistParser,
parse,
Expand All @@ -33,3 +22,9 @@ module.exports = {
writeFile,
writeFileSync,
};

export default SimplePlist;
export type { callbackFn, PlistJsObj, StringOrBuffer } from "./types";

// preserve backwards compatibility
module.exports = SimplePlist;

0 comments on commit 26d93f4

Please sign in to comment.