|
| 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