Skip to content

Commit

Permalink
chore: run linter
Browse files Browse the repository at this point in the history
Signed-off-by: Jakub Freisler <[email protected]>
  • Loading branch information
FRSgit committed Nov 12, 2022
1 parent a83addb commit 5bf169a
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 17 deletions.
37 changes: 29 additions & 8 deletions src/image.utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,22 @@ import { METADATA_KEY } from "./constants";

type PluginMetadata = {
version: string;
testingType?: 'e2e' | 'component';
testingType?: "e2e" | "component";
};

type PluginMetadataConfig = {
testingType?: string;
};

export const addPNGMetadata = (config: PluginMetadataConfig, png: Buffer) =>
addMetadata(png, METADATA_KEY, JSON.stringify({ version, testingType: config.testingType || 'e2e' } as PluginMetadata) /* c8 ignore next */);
addMetadata(
png,
METADATA_KEY,
JSON.stringify({
version,
testingType: config.testingType || "e2e",
} as PluginMetadata) /* c8 ignore next */
);
export const getPNGMetadata = (png: Buffer): PluginMetadata | undefined => {
const metadataString = getMetadata(png, METADATA_KEY /* c8 ignore next */);

Expand All @@ -28,18 +35,30 @@ export const getPNGMetadata = (png: Buffer): PluginMetadata | undefined => {
} catch {
return { version: metadataString };
}
}
};
export const isImageCurrentVersion = (png: Buffer) =>
getPNGMetadata(png)?.version === version;
export const isImageGeneratedByPlugin = (png: Buffer) =>
!!getPNGMetadata(png /* c8 ignore next */);
export const isImageOfTestType = (png: Buffer, testingType?: PluginMetadataConfig['testingType']) => {
export const isImageOfTestType = (
png: Buffer,
testingType?: PluginMetadataConfig["testingType"]
) => {
if (!isImageGeneratedByPlugin(png)) return false;
const imageTestingType = getPNGMetadata(png /* c8 ignore next */)?.testingType;
return imageTestingType === testingType || testingType === imageTestingType === undefined;
const imageTestingType = getPNGMetadata(
png /* c8 ignore next */
)?.testingType;
return (
imageTestingType === testingType ||
(testingType === imageTestingType) === undefined
);
};

export const writePNG = (config: PluginMetadataConfig, name: string, png: PNG | Buffer) =>
export const writePNG = (
config: PluginMetadataConfig,
name: string,
png: PNG | Buffer
) =>
fs.writeFileSync(
name,
addPNGMetadata(config, png instanceof PNG ? PNG.sync.write(png) : png)
Expand Down Expand Up @@ -117,7 +136,9 @@ export const alignImagesToSameSize = (
];
};

export const cleanupUnused = (config: PluginMetadataConfig & { projectRoot: string; }) => {
export const cleanupUnused = (
config: PluginMetadataConfig & { projectRoot: string }
) => {
glob
.sync("**/*.png", {
cwd: config.projectRoot,
Expand Down
27 changes: 18 additions & 9 deletions src/task.hook.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ describe("cleanupImagesTask", () => {
);
};

describe('when testing type does not match', () => {
describe("when testing type does not match", () => {
it("does not remove unused screenshot", async () => {
const { path: projectRoot } = await dir();
const screenshotPath = await writeTmpFixture(
Expand All @@ -129,14 +129,14 @@ describe("cleanupImagesTask", () => {
cleanupImagesTask({
projectRoot,
env: { pluginVisualRegressionCleanupUnusedImages: true },
testingType: 'component',
testingType: "component",
} as unknown as Cypress.PluginConfigOptions);

expect(existsSync(screenshotPath)).toBe(true);
});
});

describe('when testing type matches', () => {
describe("when testing type matches", () => {
it("does not remove used screenshot", async () => {
const { path: projectRoot } = await dir();
const screenshotPath = await writeTmpFixture(
Expand All @@ -147,7 +147,7 @@ describe("cleanupImagesTask", () => {
cleanupImagesTask({
projectRoot,
env: { pluginVisualRegressionCleanupUnusedImages: true },
testingType: 'e2e',
testingType: "e2e",
} as unknown as Cypress.PluginConfigOptions);

expect(existsSync(screenshotPath)).toBe(true);
Expand All @@ -163,7 +163,7 @@ describe("cleanupImagesTask", () => {
cleanupImagesTask({
projectRoot,
env: { pluginVisualRegressionCleanupUnusedImages: true },
testingType: 'e2e',
testingType: "e2e",
} as unknown as Cypress.PluginConfigOptions);

expect(existsSync(screenshotPath)).toBe(false);
Expand Down Expand Up @@ -205,7 +205,10 @@ describe("compareImagesTask", () => {
describe("when old screenshot exists", () => {
it("resolves with a success message", async () =>
expect(
compareImagesTask({ testingType: 'e2e' }, await generateConfig({ updateImages: true }))
compareImagesTask(
{ testingType: "e2e" },
await generateConfig({ updateImages: true })
)
).resolves.toEqual({
message:
"Image diff factor (0%) is within boundaries of maximum threshold option 0.5.",
Expand All @@ -224,7 +227,9 @@ describe("compareImagesTask", () => {
const cfg = await generateConfig({ updateImages: false });
await fs.unlink(cfg.imgOld);

await expect(compareImagesTask({ testingType: 'e2e' }, cfg)).resolves.toEqual({
await expect(
compareImagesTask({ testingType: "e2e" }, cfg)
).resolves.toEqual({
message:
"Image diff factor (0%) is within boundaries of maximum threshold option 0.5.",
imgDiff: 0,
Expand All @@ -241,7 +246,9 @@ describe("compareImagesTask", () => {
it("resolves with an error message", async () => {
const cfg = await generateConfig({ updateImages: false });

await expect(compareImagesTask({ testingType: 'e2e' }, cfg)).resolves.toMatchSnapshot();
await expect(
compareImagesTask({ testingType: "e2e" }, cfg)
).resolves.toMatchSnapshot();
});
});

Expand All @@ -250,7 +257,9 @@ describe("compareImagesTask", () => {
const cfg = await generateConfig({ updateImages: false });
await writeTmpFixture(cfg.imgNew, oldImgFixture);

await expect(compareImagesTask({ testingType: 'e2e' }, cfg)).resolves.toMatchSnapshot();
await expect(
compareImagesTask({ testingType: "e2e" }, cfg)
).resolves.toMatchSnapshot();
});
});
});
Expand Down

0 comments on commit 5bf169a

Please sign in to comment.