1
0
mirror of https://github.com/morris/vanilla-todo.git synced 2025-09-09 05:30:42 +02:00

refactor for pure functional business logic

This commit is contained in:
Morris Brodersen
2023-11-30 11:42:02 +01:00
parent 2815a1eb4c
commit dd8dc8c4af
15 changed files with 449 additions and 252 deletions

View File

@@ -0,0 +1,143 @@
import { expect, test } from '@playwright/test';
import TodoLogicModule from '../../public/scripts/TodoLogic.js';
import '../coverage.mjs';
const { TodoLogic } = TodoLogicModule;
test('TodoLogic.initTodoData', () => {
const data = TodoLogic.initTodoData(new Date(0));
expect(data).toEqual({
at: '1970-01-01',
customAt: 0,
customLists: [],
items: [],
});
});
test('TodoLogic.addTodoItem', () => {
let data = TodoLogic.initTodoData(new Date(0));
data = TodoLogic.addTodoItem(data, { label: 'foo', listId: '1970-01-01' });
expect(data).toEqual({
at: '1970-01-01',
customAt: 0,
customLists: [],
items: [
{
id: expect.stringMatching(/./),
listId: '1970-01-01',
label: 'foo',
index: 0,
done: false,
},
],
});
data = TodoLogic.addTodoItem(data, { label: 'bar', listId: '1970-01-01' });
expect(data.items).toEqual([
{
id: expect.stringMatching(/./),
listId: '1970-01-01',
label: 'foo',
index: 0,
done: false,
},
{
id: expect.stringMatching(/./),
listId: '1970-01-01',
label: 'bar',
index: 1,
done: false,
},
]);
data = TodoLogic.addTodoItem(data, { label: 'baz', listId: '1970-01-02' });
expect(data.items).toEqual([
{
id: expect.stringMatching(/./),
listId: '1970-01-01',
label: 'foo',
index: 0,
done: false,
},
{
id: expect.stringMatching(/./),
listId: '1970-01-01',
label: 'bar',
index: 1,
done: false,
},
{
id: expect.stringMatching(/./),
listId: '1970-01-02',
label: 'baz',
index: 0,
done: false,
},
]);
});
test('TodoLogic.moveTodoItem', () => {
let data = TodoLogic.initTodoData(new Date(0));
data = {
...data,
items: [
{
id: 'a',
listId: '1970-01-01',
label: 'foo',
index: 0,
done: false,
},
{
id: 'b',
listId: '1970-01-01',
label: 'bar',
index: 1,
done: false,
},
{
id: 'c',
listId: '1970-01-02',
label: 'baz',
index: 0,
done: false,
},
],
};
data = TodoLogic.moveTodoItem(data, {
id: 'a',
listId: '1970-01-01',
index: 1,
});
expect(data.items).toEqual([
{
id: 'c',
listId: '1970-01-02',
label: 'baz',
index: 0,
done: false,
},
{
id: 'b',
listId: '1970-01-01',
label: 'bar',
index: 0,
done: false,
},
{
id: 'a',
listId: '1970-01-01',
label: 'foo',
index: 1,
done: false,
},
]);
});