Skip to content

Commit

Permalink
chore: factorize tests
Browse files Browse the repository at this point in the history
  • Loading branch information
hexaltation committed Oct 9, 2024
1 parent 324bab1 commit fdf9e78
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 114 deletions.
113 changes: 36 additions & 77 deletions test/nbrowser/DocTypeConversion.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,11 @@
import { UserAPI } from "app/common/UserAPI";
//import { assert, By, driver, until } from "mocha-webdriver";
import { assert, driver, until, WebElementPromise } from "mocha-webdriver";
import { assert, driver, until } from "mocha-webdriver";
import * as gu from "test/nbrowser/gristUtils";
import { setupTestSuite } from "test/nbrowser/testUtils";
import { Button, button, element, label, option} from "test/nbrowser/elementUtils";

type TypeLabels = "Regular" | "Template" | "Tutorial";

interface Button {
click(): Promise<void>;
element(): WebElementPromise;
wait(): Promise<void>;
visible(): Promise<boolean>;
present(): Promise<boolean>;
}

describe("Document Type Conversion", function () {
this.timeout(20000);
Expand All @@ -33,10 +28,10 @@ describe("Document Type Conversion", function () {
assert.isTrue(await button.visible());
}

async function convert(from: String, to: String) {
async function convert(from: String, to: TypeLabels) {
await gu.openDocumentSettings();

// Check that Document type is from before any conversion was ever apply to It.
// Ensure that initial document type is the expected one.
assert.equal(await displayedLabel.element().getText(), from);

// Click to open the modal
Expand All @@ -45,23 +40,10 @@ describe("Document Type Conversion", function () {
// Wait for modal.
await modal.wait();

let option;

switch (to) {
case "Regular":
option = optionRegular;
break;
case "Template":
option = optionTemplate;
break;
case "Tutorial":
option = optionTutorial;
break;
}
// Select the template option
await option?.click();
// Select the desired Document type
await optionByLabel[to].click();

assert.isTrue(await option?.checked());
assert.isTrue(await optionByLabel[to]?.checked());

// Confirm the choice
await modalConfirm.click();
Expand All @@ -74,7 +56,22 @@ describe("Document Type Conversion", function () {
assert.equal(await displayedLabel.element().getText(), to);
}

it("should allow to convert from a document type to another", async function () {
async function isRegular(){
assert.isFalse(await saveCopyButton.present());
assert.isFalse(await fiddleTag.present());
}

async function isTemplate(){
await assertExistsButton(saveCopyButton, "Save Copy");
assert.isTrue(await fiddleTag.visible());
}

async function isTutorial(){
await assertExistsButton(saveCopyButton, "Save Copy");
assert.isFalse(await fiddleTag.present());
}

it("should display the modal with only the current type selected", async function () {
await gu.openDocumentSettings();
// Make sure we see the Edit button of document type conversion.
await assertExistsButton(editButton, "Edit");
Expand Down Expand Up @@ -105,38 +102,32 @@ describe("Document Type Conversion", function () {
// If the next six tests succeed so each document type can properly be converted to every other
it('should convert from Regular to Template', async function() {
await convert("Regular", "Template");
await assertExistsButton(saveCopyButton, "Save Copy");
assert.isTrue(await fiddleTag.visible());
await isTemplate();
});

it('should convert from Template to Tutorial', async function() {
await convert("Template", "Tutorial");
await assertExistsButton(saveCopyButton, "Save Copy");
assert.isFalse(await fiddleTag.present());
await isTutorial();
});

it('should convert from Tutorial to Regular', async function() {
await convert("Tutorial", "Regular");
assert.isFalse(await saveCopyButton.present());
assert.isFalse(await fiddleTag.present());
await isRegular();
});

it('should convert from Regular to Tutorial', async function() {
await convert("Regular", "Tutorial");
await assertExistsButton(saveCopyButton, "Save Copy");
assert.isFalse(await fiddleTag.present());
await isTutorial();
});

it('should convert from Tutorial to Template', async function() {
await convert("Tutorial", "Template");
await assertExistsButton(saveCopyButton, "Save Copy");
assert.isTrue(await fiddleTag.visible());
await isTemplate();
});

it('should convert from Template to Regular', async function() {
await convert("Template", "Regular");
assert.isFalse(await saveCopyButton.present());
assert.isFalse(await fiddleTag.present());
await isRegular();
});

it('should be disabled for non-owners', async function() {
Expand Down Expand Up @@ -168,50 +159,18 @@ describe("Document Type Conversion", function () {
});
});

const element = (testId: string) => ({
element() {
return driver.find(testId);
},
async wait() {
await driver.findWait(testId, 1000);
},
async visible() {
return await this.element().isDisplayed();
},
async present() {
return await this.element().isPresent();
}
});

const label = (testId: string) => ({
...element(testId),
async text() {
return this.element().getText();
},
});

const button = (testId: string) => ({
...element(testId),
async click() {
await gu.scrollIntoView(this.element());
await this.element().click();
},
});

const option = (testId: string) => ({
...button(testId),
async checked() {
return 'true' === await this.element().findClosest("label").find("input[type='checkbox']").getAttribute('checked');
}
});

const editButton = button('.test-settings-doctype-edit');
const saveCopyButton = button('.test-tb-share-action');
const displayedLabel = label('.test-settings-doctype-value');
const modal = element('.test-settings-doctype-modal');
const optionRegular = option('.test-settings-doctype-modal-option-regular');
const optionTemplate = option('.test-settings-doctype-modal-option-template');
const optionTutorial = option('.test-settings-doctype-modal-option-tutorial');
const optionByLabel = {
'Tutorial': optionTutorial,
'Template': optionTemplate,
'Regular': optionRegular
};
const modalConfirm = button('.test-settings-doctype-modal-confirm');
const modalCancel = button('.test-settings-doctype-modal-cancel');
const fiddleTag = element('.test-fiddle-tag');
38 changes: 1 addition & 37 deletions test/nbrowser/Timing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import difference from 'lodash/difference';
import { assert, driver } from "mocha-webdriver";
import * as gu from "test/nbrowser/gristUtils";
import { setupTestSuite } from "test/nbrowser/testUtils";
import { button, element, label, option } from "test/nbrowser/elementUtils";

describe("Timing", function () {
this.timeout(20000);
Expand Down Expand Up @@ -192,43 +193,6 @@ describe("Timing", function () {
});
});

const element = (testId: string) => ({
element() {
return driver.find(testId);
},
async wait() {
await driver.findWait(testId, 1000);
},
async visible() {
return await this.element().isDisplayed();
},
async present() {
return await this.element().isPresent();
}
});

const label = (testId: string) => ({
...element(testId),
async text() {
return this.element().getText();
},
});

const button = (testId: string) => ({
...element(testId),
async click() {
await gu.scrollIntoView(this.element());
await this.element().click();
},
});

const option = (testId: string) => ({
...button(testId),
async checked() {
return 'true' === await this.element().findClosest("label").find("input[type='checkbox']").getAttribute('checked');
}
});

const startTiming = button(".test-settings-timing-start");
const stopTiming = button(".test-settings-timing-stop");
const timingText = label(".test-settings-timing-desc");
Expand Down
48 changes: 48 additions & 0 deletions test/nbrowser/elementUtils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { driver, WebElementPromise } from "mocha-webdriver";
import * as gu from "test/nbrowser/gristUtils";

export interface Button {
click(): Promise<void>;
element(): WebElementPromise;
wait(): Promise<void>;
visible(): Promise<boolean>;
present(): Promise<boolean>;
}

export const element = (testId: string) => ({
element() {
return driver.find(testId);
},
async wait() {
await driver.findWait(testId, 1000);
},
async visible() {
return await this.element().isDisplayed();
},
async present() {
return await this.element().isPresent();
}
});

export const label = (testId: string) => ({
...element(testId),
async text() {
return this.element().getText();
},
});

export const button = (testId: string): Button => ({
...element(testId),
async click() {
await gu.scrollIntoView(this.element());
await this.element().click();
},
});

export const option = (testId: string) => ({
...button(testId),
async checked() {
return 'true' === await this.element().findClosest("label").find("input[type='checkbox']").getAttribute('checked');
}
});

0 comments on commit fdf9e78

Please sign in to comment.