Skip to content

Commit b3a22f1

Browse files
committed
Add integration tests for buttons with Jest
1 parent d4842e5 commit b3a22f1

File tree

4 files changed

+255
-12
lines changed

4 files changed

+255
-12
lines changed

.eslintrc.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@
5555
{
5656
"files": ["src/**/*"],
5757
"plugins": ["react-hooks"],
58-
"extends": ["plugin:react/recommended"],
58+
"extends": ["plugin:react/recommended", "plugin:jest-dom/recommended"],
5959
"env": {
6060
"browser": true,
6161
"node": false

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,11 @@
2727
"@babel/plugin-transform-modules-commonjs": "7.7.0",
2828
"@babel/preset-react": "7.7.0",
2929
"@ffflorian/jszip-cli": "3.0.2",
30+
"@testing-library/jest-dom": "4.2.0",
3031
"babel-loader": "8.0.6",
3132
"eslint": "6.6.0",
3233
"eslint-config-prettier": "6.5.0",
34+
"eslint-plugin-jest-dom": "1.1.3",
3335
"eslint-plugin-prettier": "3.1.1",
3436
"eslint-plugin-react": "7.16.0",
3537
"eslint-plugin-react-hooks": "2.2.0",

src/content/index.test.js

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import "@testing-library/jest-dom/extend-expect";
2+
import GitHub from "./github";
3+
import StackOverflow from "./stackOverflow";
4+
import Storage from "./storage";
5+
6+
window.chrome = {
7+
runtime: {},
8+
storage: {
9+
onChanged: {
10+
addListener() {}
11+
},
12+
sync: {
13+
get(callback) {
14+
setTimeout(() => callback({}));
15+
}
16+
}
17+
}
18+
};
19+
20+
window.MutationObserver = class {
21+
constructor() {}
22+
observe() {}
23+
};
24+
25+
describe("Prettier format button injection", () => {
26+
function createStorage() {
27+
const storage = new Storage();
28+
return storage.init();
29+
}
30+
31+
function expectToHavePrettierButton() {
32+
expect(document.querySelector(".prettier-btn")).toHaveTextContent(
33+
"Prettier"
34+
);
35+
}
36+
37+
beforeEach(() => (document.body.innerHTML = ""));
38+
39+
test("GitHub", async () => {
40+
// Basis: https://github.com/prettier/prettier-chrome-extension/issues/new
41+
const button = document.createElement("button");
42+
button.innerText = "Comment";
43+
document.body.appendChild(button);
44+
// Hack around JSDOM's lack of offsetHeight support to fix isElementVisible
45+
Object.defineProperty(document.body, "offsetHeight", { value: 1 });
46+
47+
new GitHub(await createStorage());
48+
expectToHavePrettierButton();
49+
});
50+
51+
test("Stack Overflow", async () => {
52+
// Basis: https://stackoverflow.com/questions/51875054
53+
const button = document.createElement("div");
54+
button.className = "wmd-button-row";
55+
document.body.appendChild(button);
56+
57+
new StackOverflow(await createStorage());
58+
expectToHavePrettierButton();
59+
});
60+
});

0 commit comments

Comments
 (0)