Skip to content

Commit e788575

Browse files
authored
Feat/improve design (#59)
* feat: add hook to add margin to the sidebar * test: add sidebar tests * feat: add better selector handling and update tests
1 parent 6e635a2 commit e788575

8 files changed

Lines changed: 64 additions & 10 deletions

File tree

src/initializers/initSidebar.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ interface Options {
2020
export const initSidebar = (options: Options) => {
2121
const { Component, createRoot } = options;
2222
const el = document.createElement('div');
23+
el.setAttribute('id', 'twd-sidebar-root');
2324
document.body.appendChild(el);
2425
const root = createRoot(el);
2526
root.render(Component);

src/tests/e2e/api/get.spec.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,15 @@ describe('twd get command', () => {
66

77
beforeEach(() => {
88
document.body.innerHTML = '';
9+
// Create a wrapper div that would be the app container (not the sidebar)
10+
const appContainer = document.createElement('div');
11+
appContainer.id = 'app';
12+
document.body.appendChild(appContainer);
13+
14+
// append a div inside the app container
915
div = document.createElement('div');
10-
document.body.appendChild(div);
16+
div.className = 'inner';
17+
appContainer.appendChild(div);
1118
});
1219

1320
it('should get element', async () => {

src/tests/e2e/api/getAll.spec.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,25 @@ describe('twd getAll command', () => {
88

99
beforeEach(() => {
1010
document.body.innerHTML = '';
11+
// Create a wrapper div that would be the app container (not the sidebar)
12+
const container = document.createElement('div');
13+
container.id = 'app';
14+
document.body.appendChild(container);
15+
1116
item1 = document.createElement('div');
1217
item1.className = 'item';
1318
item1.textContent = 'Hello';
14-
document.body.appendChild(item1);
19+
container.appendChild(item1);
1520

1621
item2 = document.createElement('div');
1722
item2.className = 'item';
1823
item2.textContent = 'World';
19-
document.body.appendChild(item2);
24+
container.appendChild(item2);
2025

2126
item3 = document.createElement('div');
2227
item3.className = 'item';
2328
item3.textContent = '!';
24-
document.body.appendChild(item3);
29+
container.appendChild(item3);
2530
});
2631

2732
it('should get all elements', async () => {

src/tests/e2e/api/should.spec.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,13 @@ describe('twd should', () => {
66

77
beforeEach(() => {
88
document.body.innerHTML = '';
9+
// Create a wrapper div that would be the app container (not the sidebar)
10+
const appContainer = document.createElement('div');
11+
appContainer.id = 'app';
12+
document.body.appendChild(appContainer);
13+
914
div = document.createElement('div');
10-
document.body.appendChild(div);
15+
appContainer.appendChild(div);
1116
});
1217

1318
it('should assert have.text', async () => {

src/tests/ui/twdSidebar.spec.tsx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ describe("TWDSidebar", () => {
1919
// position fixed but right not left
2020
expect(sidebarElement).toHaveStyle({ position: 'fixed', right: '0' });
2121
expect(sidebarElement).not.toHaveStyle({ position: 'fixed', left: '0' });
22+
// html margin right 280px
23+
expect(document.documentElement).toHaveStyle({ marginRight: '280px' });
24+
expect(document.documentElement).not.toHaveStyle({ marginLeft: '280px' });
2225
});
2326

2427
it("should render TWDSidebar component closed", async () => {

src/twd.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -210,8 +210,10 @@ interface TWDAPI {
210210
*/
211211
export const twd: TWDAPI = {
212212
get: async (selector: string): Promise<TWDElemAPI> => {
213+
// Prepend selector to exclude TWD sidebar elements
214+
const enhancedSelector = `body > div:not(#twd-sidebar-root) ${selector}`;
213215
log(`Searching get("${selector}")`);
214-
const el = await waitForElement(() => document.querySelector(selector));
216+
const el = await waitForElement(() => document.querySelector(enhancedSelector));
215217

216218
const api: TWDElemAPI = {
217219
el,
@@ -224,14 +226,16 @@ export const twd: TWDAPI = {
224226
return api;
225227
},
226228
getAll: async (selector: string): Promise<TWDElemAPI[]> => {
229+
// Prepend selector to exclude TWD sidebar elements
230+
const enhancedSelector = `body > div:not(#twd-sidebar-root) ${selector}`;
227231
log(`Searching getAll("${selector}")`);
228-
const els = await waitForElements(() => document.querySelectorAll(selector));
232+
const els = await waitForElements(() => document.querySelectorAll(enhancedSelector));
229233

230234
return els.map((el) => {
231235
const api: TWDElemAPI = {
232-
el,
236+
el: el as HTMLElement,
233237
should: (name: AnyAssertion, ...args: ArgsFor<AnyAssertion>) => {
234-
const message = runAssertion(el, name, ...args);
238+
const message = runAssertion(el as HTMLElement, name, ...args);
235239
log(message);
236240
return api;
237241
},

src/ui/TWDSidebar.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
import { useState } from "react";
1+
import { useEffect, useState } from "react";
22
import { tests } from "../twdRegistry";
33
import { TestList } from "./TestList";
44
import { ClosedSidebar } from "./ClosedSidebar";
5+
import { useLayout } from "./hooks/useLayout";
56

67
interface TWDSidebarProps {
78
/**
@@ -29,6 +30,7 @@ export const TWDSidebar = ({ open, position = "left" }: TWDSidebarProps) => {
2930
const [_, setRefresh] = useState(0);
3031
const [isOpen, setIsOpen] = useState(open);
3132
const [filter, setFilter] = useState("");
33+
useLayout({ isOpen, position });
3234

3335
const runTest = async (i: number) => {
3436
const test = tests[i];

src/ui/hooks/useLayout.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import { useEffect } from "react";
2+
3+
interface UseLayoutProps {
4+
isOpen: boolean;
5+
position: "left" | "right";
6+
}
7+
8+
export const useLayout = ({ isOpen, position }: UseLayoutProps) => {
9+
useEffect(() => {
10+
// based on isOpen add html tag margin and based on position
11+
const html = document.documentElement;
12+
if (isOpen) {
13+
if (position === "left") {
14+
html.style.marginLeft = "280px";
15+
} else {
16+
html.style.marginRight = "280px";
17+
}
18+
} else {
19+
html.style.marginRight = "0";
20+
html.style.marginLeft = "0";
21+
}
22+
return () => {
23+
html.style.marginRight = "0";
24+
html.style.marginLeft = "0";
25+
};
26+
}, [isOpen, position]);
27+
};

0 commit comments

Comments
 (0)