Skip to content

Commit

Permalink
test: add nested components
Browse files Browse the repository at this point in the history
  • Loading branch information
sebkolind committed Oct 22, 2024
1 parent e743f99 commit 520d179
Showing 1 changed file with 46 additions and 0 deletions.
46 changes: 46 additions & 0 deletions src/__tests__/walker.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,4 +110,50 @@ describe('walker', () => {
expect(getByText(document.body, /one/)).toBeDefined();
expect(document.querySelectorAll('li').length).toBe(1);
});

test('nested components', () => {
type State = { count: number };

const InnerComponent: Component<State> = {
state: { count: 0 },
view: ({ state }) =>
button(`Click me inner ${state.count}`, {
onclick: () => state.count++,
}),
};

const OuterComponent: Component<State> = {
state: { count: 0 },
view: ({ state }) =>
div([
div([], { id: 'inner' }),
button(`Click me outer ${state.count}`, {
onclick: () => state.count++,
}),
]),
mounted() {
mount(document.getElementById('inner')!, InnerComponent);
},
};

mount(document.body, OuterComponent);

const outerBtn = getByText(document.body, /Click me outer 0/);

expect(outerBtn).toBeDefined();

fireEvent.click(outerBtn);
fireEvent.click(outerBtn);
fireEvent.click(outerBtn);

const innerBtn = getByText(document.body, /Click me inner 0/);

expect(innerBtn).toBeDefined();

fireEvent.click(innerBtn);
fireEvent.click(innerBtn);

expect(getByText(document.body, /Click me inner 2/)).toBeDefined();
expect(getByText(document.body, /Click me outer 3/)).toBeDefined();
});
});

0 comments on commit 520d179

Please # to comment.