Skip to content

Commit 3b63068

Browse files
tests: refactor slide style guide tests and add playground size checks (#2745)
The previous slide-size.test.ts was refactored and a slide class was implemented for convenience. The test now checks the playground size (if available on that slide) by checking if scrollbars are visible. To make this test more efficient, the mocha before() hook is used to load the slide once and check with all relevant tests before loading the next slide.
1 parent c3450e7 commit 3b63068

File tree

4 files changed

+140
-54
lines changed

4 files changed

+140
-54
lines changed

tests/src/objects/slide.ts

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import { $, browser } from "@wdio/globals";
2+
3+
export default class Slide {
4+
/**
5+
* convenience functions for interacting with a slide
6+
**/
7+
8+
/**
9+
* @returns the scrollbar of the provided type if available
10+
*/
11+
scrollbar_typed(scrollbar_type: string): ChainablePromiseElement {
12+
return $("#content main div.ace_scrollbar-" + scrollbar_type);
13+
}
14+
15+
get scrollbar_v() {
16+
return this.scrollbar_typed("v");
17+
}
18+
19+
get scrollbar_h() {
20+
return this.scrollbar_typed("h");
21+
}
22+
23+
get main_content() {
24+
return $("#content > main");
25+
}
26+
27+
/**
28+
*
29+
* @param element the element to be checked
30+
* @param height the maximum height
31+
* @param width the maximum width
32+
* @returns true if either height or width is higher than the provided numbers
33+
*/
34+
async violates_max_size(
35+
element: ChainablePromiseElement,
36+
height: number,
37+
width: number,
38+
): Promise<boolean> {
39+
const main_element_size = await element.getSize();
40+
return (
41+
main_element_size.height >= height || main_element_size.width > width
42+
);
43+
}
44+
45+
async load(slide_path: string) {
46+
// ensure this is prefixed with /
47+
if (!slide_path.startsWith("/")) {
48+
slide_path = "/" + slide_path;
49+
}
50+
return await browser.url(slide_path);
51+
}
52+
}

tests/src/slide-size.test.ts

Lines changed: 0 additions & 53 deletions
This file was deleted.

tests/src/slide-style-guide.test.ts

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
/**
2+
* This file contains tests that check the style of the slides.
3+
* It checks that the slides are not too high or wide and that
4+
* the code examples are not too wide or high based on the visibility of scrollbars.
5+
*
6+
* Slides that exist on the exemptions lists are tested for that violation
7+
* and if they are not violating the style, this alerts and the author should remove
8+
* the slide from the exemption list. This acts as a regression check.
9+
*/
10+
import { describe, it } from "mocha";
11+
import { expect } from "@wdio/globals";
12+
import { slides } from "./slides/slides.list.ts";
13+
import {
14+
playground_size_exemptions,
15+
size_exemptions,
16+
} from "./slides/slide-exemptions.list.ts";
17+
import Slide from "./objects/slide.ts";
18+
19+
// these are empirically determined values in 16:9 ratio
20+
const MAX_HEIGHT = 1333;
21+
const MAX_WIDTH = 750;
22+
23+
const slide = new Slide();
24+
slides.forEach((slide_path) => {
25+
describe("Slide " + slide_path, () => {
26+
before(async () => {
27+
slide.load(slide_path);
28+
});
29+
30+
// slide size evaluation
31+
if (size_exemptions.includes(slide_path)) {
32+
// This slide is exempted and violated rules before.
33+
// It is expected to still do this and if not it should be removed from exemptions.
34+
// This acts as a regression check
35+
it("is on the exemption list but should be removed from size_exemptions in slide-exemptions.list.ts", async () => {
36+
const main_element = slide.main_content;
37+
console.info("slide " + slide_path + " is on the exemption list");
38+
expect(
39+
await slide.violates_max_size(main_element, MAX_HEIGHT, MAX_WIDTH),
40+
).toBe(true);
41+
});
42+
} else {
43+
it(
44+
"should not be higher than " +
45+
MAX_HEIGHT +
46+
" pixels or wider than " +
47+
MAX_WIDTH +
48+
" pixels",
49+
async () => {
50+
const main_element = slide.main_content;
51+
expect(
52+
await slide.violates_max_size(main_element, MAX_HEIGHT, MAX_WIDTH),
53+
).toBe(false);
54+
},
55+
);
56+
}
57+
58+
// playground code examples are not too wide
59+
if (playground_size_exemptions.includes(slide_path)) {
60+
it("is on the exemption list but should be removed from playground_size_exemptions in slide-exemptions.list.ts", async () => {
61+
// This slide is exempted and violated rules before.
62+
// It is expected to still do this and if not it should be removed from exemptions.
63+
// This acts as a regression check
64+
await Promise.any([
65+
expect(slide.scrollbar_h).toBeDisplayed(),
66+
expect(slide.scrollbar_v).toBeDisplayed(),
67+
]);
68+
});
69+
} else {
70+
it("should not show a scrollbar", async () => {
71+
if (await slide.has_code_example) {
72+
await Promise.all([
73+
expect(slide.scrollbar_h).not.toBeDisplayed(),
74+
expect(slide.scrollbar_v).not.toBeDisplayed(),
75+
]);
76+
}
77+
});
78+
}
79+
});
80+
});

tests/src/slides/slide-exemptions.list.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// These slides are known to violate the slide style guide.
22
// They are checked if they still violate and if not fail the test.
33
// Please remove slides that become good so they don't regress.
4-
export const exemptions = [
4+
export const size_exemptions = [
55
"android/interoperability/java.html",
66
"android/testing.html",
77
"bare-metal/aps/entry-point.html",
@@ -16,3 +16,10 @@ export const exemptions = [
1616
"concurrency/sync-exercises/solutions.html",
1717
"concurrency/sync-exercises/link-checker.html",
1818
];
19+
20+
export const playground_size_exemptions = [
21+
"bare-metal/aps/better-uart/driver.html",
22+
"bare-metal/microcontrollers/type-state.html",
23+
"concurrency/async-pitfalls/cancellation.html",
24+
"iterators/intoiterator.html",
25+
];

0 commit comments

Comments
 (0)