Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions cypress/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ import {
ContentItem,
ContentModel,
ContentModelField,
WebView,
Stylesheet,
Script,
} from "../src/shell/services/types";
import "./support/commands";

Expand Down Expand Up @@ -65,6 +68,12 @@ declare global {
fields: Partial<ContentModelField>;
items: Partial<ContentItem>;
}>;
task(
event: "seed:code",
path: string
): Chainable<
Partial<WebView> | Partial<Script> | Partial<Stylesheet> | null
>;
}
}
}
2 changes: 2 additions & 0 deletions cypress/plugins/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ const path = require("path");
const dotenv = require("dotenv");
const os = require("os");
const content = require("./seeds/content");
const code = require("./seeds/code");
const codeCoverageTask = require("@cypress/code-coverage/task");

module.exports = (on, config) => {
Expand All @@ -28,6 +29,7 @@ module.exports = (on, config) => {
return null;
},
...content(config),
...code(config),
});
// `on` is used to hook into various events Cypress emits
// `config` is the resolved Cypress config
Expand Down
61 changes: 61 additions & 0 deletions cypress/plugins/seeds/code.ts

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What happens if the api call fails?

Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import { readFileSync } from "fs";
import { join } from "path";

import { WebView, Stylesheet, Script } from "../../../src/shell/services/types";

export type SeedCodeTask = WebView | Stylesheet | Script | null;
const STYLESHEET_TYPES = ["text/css", "text/less", "text/scss", "text/sass"];

module.exports = function code(config) {
const { getSDK, getAuthToken } = require("./utils");

async function seedCode(path: string): Promise<SeedCodeTask> {
const jsonString = readFileSync(join(__dirname, "../../", path), "utf8");
const json = JSON.parse(jsonString);

const sdk = await getSDK(config);
const timeStamp = Date.now();
const filename = `/__e2e__/${config.env.COMMIT_ID}/${timeStamp} | ${json?.filename}`;

let responseData = null;

if (json.type === "text/javascript") {
const token = await getAuthToken();
await fetch(`${config.env.API_INSTANCE_URL}/web/scripts`, {
method: "POST",
headers: {
authorization: `Bearer ${token}`,
},
body: JSON.stringify({ ...json, filename }),
}).then(async (res) => {
const resJson = await res.json();
responseData = resJson?.data;
});
} else if (STYLESHEET_TYPES.includes(json.type)) {
await sdk.instance
.createStylesheet({
...json,
filename,
})
.then((res) => {
responseData = res?.data;
});
} else {
await sdk.instance
.createView({
...json,
filename,
})
.then((res) => {
responseData = res?.data;
});
}

return responseData;
}

// CODE TASK MAPPING
return {
"seed:code": (path: string) => seedCode(path),
};
};
Loading