Skip to content

Commit

Permalink
test(view): add tests for lists
Browse files Browse the repository at this point in the history
  • Loading branch information
lorenzofox3 committed Dec 26, 2023
1 parent 242ef05 commit ff6984c
Show file tree
Hide file tree
Showing 2 changed files with 182 additions and 0 deletions.
181 changes: 181 additions & 0 deletions packages/view/test/list.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,181 @@
import { test } from '@cofn/test-lib/client';
import { fromView, nextTick } from './utils.js';

const debug = document.getElementById('debug');

test('list is rendered and item within the list is updated', ({ eq }) => {
const el = fromView(
({ html }) =>
({ items = [] }) =>
// prettier-ignore
html`<ul>${items.map(({ content, id }) => html`${id}::<li>${content}</li>`)}</ul>`,
);

debug.appendChild(el);
eq(el.innerHTML, '<ul><!--before--><!--after--></ul>');
el.render({
items: [
{ id: 1, content: 'item1' },
{ id: 2, content: 'item2' },
{ id: 3, content: 'item3' },
],
});

eq(
el.innerHTML,
'<ul><!--before--><li>item1</li><li>item2</li><li>item3</li><!--after--></ul>',
);

el.render({
items: [
{ id: 1, content: 'item1' },
{ id: 2, content: 'item2bis' },
{ id: 3, content: 'item3' },
],
});

eq(
el.innerHTML,
'<ul><!--before--><li>item1</li><li>item2bis</li><li>item3</li><!--after--></ul>',
);
});

test('list is rendered and item within list is added', ({ eq }) => {
const el = fromView(
({ html }) =>
({ items = [] }) =>
// prettier-ignore
html`<ul>${items.map(({ content, id }) => html`${id}::<li>${content}</li>`)}</ul>`,
);

debug.appendChild(el);
eq(el.innerHTML, '<ul><!--before--><!--after--></ul>');
el.render({
items: [
{ id: 1, content: 'item1' },
{ id: 2, content: 'item2' },
{ id: 3, content: 'item3' },
],
});

eq(
el.innerHTML,
'<ul><!--before--><li>item1</li><li>item2</li><li>item3</li><!--after--></ul>',
);

el.render({
items: [
{ id: 1, content: 'item1' },
{ id: 11, content: 'item11' },
{ id: 2, content: 'item2' },
{ id: 3, content: 'item3' },
],
});

eq(
el.innerHTML,
'<ul><!--before--><li>item1</li><li>item11</li><li>item2</li><li>item3</li><!--after--></ul>',
);
});

test('list is rendered and item within list is removed', ({ eq }) => {
const el = fromView(
({ html }) =>
({ items = [] }) =>
// prettier-ignore
html`<ul>${items.map(({ content, id }) => html`${id}::<li>${content}</li>`)}</ul>`,
);

debug.appendChild(el);
eq(el.innerHTML, '<ul><!--before--><!--after--></ul>');
el.render({
items: [
{ id: 1, content: 'item1' },
{ id: 2, content: 'item2' },
{ id: 3, content: 'item3' },
],
});

eq(
el.innerHTML,
'<ul><!--before--><li>item1</li><li>item2</li><li>item3</li><!--after--></ul>',
);

el.render({
items: [
{ id: 1, content: 'item1' },
{ id: 3, content: 'item3' },
],
});

eq(
el.innerHTML,
'<ul><!--before--><li>item1</li><li>item3</li><!--after--></ul>',
);
});

test('list within a list is handled', ({ eq }) => {
const el = fromView(
({ html }) =>
({ items = [] }) =>
// prettier-ignore
html`<ul>${items.map(({ children, id }) => html`${id}::<li><ul>${children.map(({ content, id: childId }) => html`${id+'-'+childId}::<li>${content}</li>`)}</ul></li>`)}</ul>`,
);

debug.appendChild(el);
eq(el.innerHTML, '<ul><!--before--><!--after--></ul>');
el.render({
items: [
{
id: 1,
children: [
{ content: 'item1', id: 'ch-1' },
{ content: 'item2', id: 'ch-2' },
],
},
{ id: 2, children: [{ content: 'item11', id: 'ch-11' }] },
{
id: 3,
children: [
{ content: 'item3', id: 'ch-3' },
{ content: 'item4', id: 'ch-4' },
{ content: 'item5', id: 'ch-5' },
],
},
],
});

eq(
el.innerHTML,
'<ul><!--before--><li><ul><!--before--><li>item1</li><li>item2</li><!--after--></ul></li><li><ul><!--before--><li>item11</li><!--after--></ul></li><li><ul><!--before--><li>item3</li><li>item4</li><li>item5</li><!--after--></ul></li><!--after--></ul>',
);

el.render({
items: [
{
id: 1,
children: [{ content: 'item1', id: 'ch-1' }],
},
{
id: 2,
children: [
{ content: 'item11', id: 'ch-11' },
{ content: 'item2', id: 'ch-2' },
],
},
{
id: 3,
children: [
{ content: 'item3', id: 'ch-3' },
{ content: 'item4bis', id: 'ch-4' },
{ content: 'item5', id: 'ch-5' },
],
},
],
});

eq(
el.innerHTML,
'<ul><!--before--><li><ul><!--before--><li>item1</li><!--after--></ul></li><li><ul><!--before--><li>item11</li><li>item2</li><!--after--></ul></li><li><ul><!--before--><li>item3</li><li>item4bis</li><li>item5</li><!--after--></ul></li><!--after--></ul>',
);
});
1 change: 1 addition & 0 deletions packages/view/test/test-suite.html
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ <h1>Test reporting</h1>
import './attributes.test.js';
import './event-listeners.test.js'
import './if-dom.test.js'
import './list.test.js';

const [st1, st2] = report().tee();
st1.pipeTo(createSocketSink(import.meta.hot));
Expand Down

0 comments on commit ff6984c

Please sign in to comment.