rewrite existing cypress tests to playwright (#931)
This commit is contained in:
1 parent
5c4f4bfd90
commit
dc4331ed17
24 files changed
+346
-689
No files matched your search
@@ -0,0 +1,34 @@
|
||||
import { kont } from "kont";
|
||||
|
||||
import { pageProvider } from "./lib/pageProvider";
|
||||
|
||||
jest.setTimeout(60e3);
|
||||
|
||||
describe("free user", () => {
|
||||
const ctx = kont()
|
||||
.useBeforeEach(pageProvider({ path: "/free" }))
|
||||
.done();
|
||||
|
||||
test("only one visible event", async () => {
|
||||
const { page } = ctx;
|
||||
await expect(page).toHaveSelector(`[href="/free/30min"]`);
|
||||
await expect(page).not.toHaveSelector(`[href="/free/60min"]`);
|
||||
});
|
||||
|
||||
// TODO: make sure `/free/30min` is bookable and that `/free/60min` is not
|
||||
});
|
||||
|
||||
describe("pro user", () => {
|
||||
const ctx = kont()
|
||||
.useBeforeEach(pageProvider({ path: "/pro" }))
|
||||
.done();
|
||||
|
||||
test("pro user's page has at least 2 visible events", async () => {
|
||||
const { page } = ctx;
|
||||
const $eventTypes = await page.$$("[data-testid=event-types] > *");
|
||||
|
||||
expect($eventTypes.length).toBeGreaterThanOrEqual(2);
|
||||
});
|
||||
|
||||
// TODO: make sure `/free/30min` is bookable and that `/free/60min` is not
|
||||
});
|
||||
@@ -0,0 +1,78 @@
|
||||
import { kont } from "kont";
|
||||
|
||||
import { loginProvider } from "./lib/loginProvider";
|
||||
import { randomString } from "./lib/testUtils";
|
||||
|
||||
jest.setTimeout(60e3);
|
||||
|
||||
describe("pro user", () => {
|
||||
const ctx = kont()
|
||||
.useBeforeEach(
|
||||
loginProvider({
|
||||
user: "pro",
|
||||
path: "/event-types",
|
||||
waitForSelector: "[data-testid=event-types]",
|
||||
})
|
||||
)
|
||||
.done();
|
||||
|
||||
test("has at least 2 events", async () => {
|
||||
const { page } = ctx;
|
||||
const $eventTypes = await page.$$("[data-testid=event-types] > *");
|
||||
|
||||
expect($eventTypes.length).toBeGreaterThanOrEqual(2);
|
||||
for (const $el of $eventTypes) {
|
||||
expect(await $el.getAttribute("data-disabled")).toBe("0");
|
||||
}
|
||||
});
|
||||
|
||||
test("can add new event type", async () => {
|
||||
const { page } = ctx;
|
||||
await page.click("[data-testid=new-event-type]");
|
||||
const nonce = randomString(3);
|
||||
const eventTitle = `hello ${nonce}`;
|
||||
|
||||
await page.fill("[name=title]", eventTitle);
|
||||
await page.fill("[name=length]", "10");
|
||||
await page.click("[type=submit]");
|
||||
|
||||
await page.waitForNavigation({
|
||||
url(url) {
|
||||
return url.pathname !== "/event-types";
|
||||
},
|
||||
});
|
||||
|
||||
await page.goto("http://localhost:3000/event-types");
|
||||
|
||||
await expect(page).toHaveSelector(`text='${eventTitle}'`);
|
||||
});
|
||||
});
|
||||
|
||||
describe("free user", () => {
|
||||
const ctx = kont()
|
||||
.useBeforeEach(
|
||||
loginProvider({
|
||||
user: "free",
|
||||
path: "/event-types",
|
||||
waitForSelector: "[data-testid=event-types]",
|
||||
})
|
||||
)
|
||||
.done();
|
||||
|
||||
test("has at least 2 events where first is enabled", async () => {
|
||||
const { page } = ctx;
|
||||
const $eventTypes = await page.$$("[data-testid=event-types] > *");
|
||||
|
||||
expect($eventTypes.length).toBeGreaterThanOrEqual(2);
|
||||
const [$first] = $eventTypes;
|
||||
const $last = $eventTypes.pop()!;
|
||||
expect(await $first.getAttribute("data-disabled")).toBe("0");
|
||||
expect(await $last.getAttribute("data-disabled")).toBe("1");
|
||||
});
|
||||
|
||||
test("can not add new event type", async () => {
|
||||
const { page } = ctx;
|
||||
|
||||
await expect(page.$("[data-testid=new-event-type]")).toBeDisabled();
|
||||
});
|
||||
});
|
||||
@@ -1,23 +0,0 @@
|
||||
jest.setTimeout(35e3);
|
||||
|
||||
test("go to /pro", async () => {
|
||||
// Go to http://localhost:3000/auth/login
|
||||
await page.goto("http://localhost:3000/auth/login");
|
||||
// Click input[name="email"]
|
||||
await page.click('input[name="email"]');
|
||||
// Fill input[name="email"]
|
||||
await page.fill('input[name="email"]', "[email protected]");
|
||||
// Press Tab
|
||||
await page.press('input[name="email"]', "Tab");
|
||||
// Fill input[name="password"]
|
||||
await page.fill('input[name="password"]', "pro");
|
||||
// Press Enter
|
||||
await page.press('input[name="password"]', "Enter");
|
||||
|
||||
await page.waitForSelector("[data-testid=event-types]");
|
||||
|
||||
const $eventTypes = await page.$$("[data-testid=event-types] > *");
|
||||
expect($eventTypes.length).toBeGreaterThanOrEqual(2);
|
||||
});
|
||||
|
||||
export {};
|
||||
@@ -0,0 +1,75 @@
|
||||
/* eslint-disable @typescript-eslint/ban-types */
|
||||
import { provider, Provider } from "kont";
|
||||
import { Page } from "playwright";
|
||||
|
||||
/**
|
||||
* Context data that Login provder needs.
|
||||
*/
|
||||
export type Needs = {};
|
||||
|
||||
/**
|
||||
* Login provider's options.
|
||||
*/
|
||||
export type Params = {
|
||||
user: string;
|
||||
};
|
||||
|
||||
/**
|
||||
* Context data that Page provider contributes.
|
||||
*/
|
||||
export type Contributes = {
|
||||
page: Page;
|
||||
};
|
||||
|
||||
/**
|
||||
* Creates a new context / "incognito tab" and logs in the specified user
|
||||
*/
|
||||
export function loginProvider(opts: {
|
||||
user: string;
|
||||
/**
|
||||
* Path to navigate to after login
|
||||
*/
|
||||
path?: string;
|
||||
/**
|
||||
* Selector to wait for to decide that the navigation is done
|
||||
*/
|
||||
waitForSelector?: string;
|
||||
}): Provider<Needs, Contributes> {
|
||||
return provider<Needs, Contributes>()
|
||||
.name("page")
|
||||
.before(async () => {
|
||||
const context = await browser.newContext();
|
||||
const page = await context.newPage();
|
||||
|
||||
await page.goto("http://localhost:3000/event-types");
|
||||
// Click input[name="email"]
|
||||
await page.click('input[name="email"]');
|
||||
// Fill input[name="email"]
|
||||
await page.fill('input[name="email"]', `${opts.user}@example.com`);
|
||||
// Press Tab
|
||||
await page.press('input[name="email"]', "Tab");
|
||||
// Fill input[name="password"]
|
||||
await page.fill('input[name="password"]', opts.user);
|
||||
// Press Enter
|
||||
await page.press('input[name="password"]', "Enter");
|
||||
|
||||
await page.waitForSelector("[data-testid=event-types]");
|
||||
|
||||
if (opts.path) {
|
||||
await page.goto(`http://localhost:3000${opts.path}`);
|
||||
}
|
||||
if (opts.waitForSelector) {
|
||||
await page.waitForSelector(opts.waitForSelector);
|
||||
}
|
||||
|
||||
return {
|
||||
page,
|
||||
context,
|
||||
};
|
||||
})
|
||||
.after(async (ctx) => {
|
||||
await ctx.page?.close();
|
||||
await ctx.context?.close();
|
||||
})
|
||||
.done();
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
/* eslint-disable @typescript-eslint/ban-types */
|
||||
import { provider, Provider } from "kont";
|
||||
import { Page } from "playwright";
|
||||
|
||||
/**
|
||||
* Context data that Page provder needs.
|
||||
*/
|
||||
export type Needs = {};
|
||||
|
||||
/**
|
||||
* Page provider's options.
|
||||
*/
|
||||
export type Params = {
|
||||
user: string;
|
||||
};
|
||||
|
||||
/**
|
||||
* Context data that Page provider contributes.
|
||||
*/
|
||||
export type Contributes = {
|
||||
page: Page;
|
||||
};
|
||||
|
||||
/**
|
||||
* Creates a new context / "incognito tab" and logs in the specified user
|
||||
*/
|
||||
export function pageProvider(opts: {
|
||||
/**
|
||||
* Path to navigate to
|
||||
*/
|
||||
path: string;
|
||||
}): Provider<Needs, Contributes> {
|
||||
return provider<Needs, Contributes>()
|
||||
.name("page")
|
||||
.before(async () => {
|
||||
const context = await browser.newContext();
|
||||
const page = await context.newPage();
|
||||
|
||||
await page.goto(`http://localhost:3000${opts.path}`);
|
||||
|
||||
return {
|
||||
page,
|
||||
context,
|
||||
};
|
||||
})
|
||||
.after(async (ctx) => {
|
||||
await ctx.page?.close();
|
||||
await ctx.context?.close();
|
||||
})
|
||||
.done();
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
export function randomString(length: number) {
|
||||
let result = "";
|
||||
const characters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
|
||||
const charactersLength = characters.length;
|
||||
for (let i = 0; i < length; i++) {
|
||||
result += characters.charAt(Math.floor(Math.random() * charactersLength));
|
||||
}
|
||||
return result;
|
||||
}
|
||||
Reference in new issue
Block a user