Skip to content

Commit

Permalink
test(store): add extensive tests for array strore methods (root level…
Browse files Browse the repository at this point in the history
… array)
  • Loading branch information
DawidWraga committed May 22, 2024
1 parent e0132ab commit 960ba51
Show file tree
Hide file tree
Showing 3 changed files with 222 additions and 9 deletions.
89 changes: 80 additions & 9 deletions packages/store/test/array-store.test.tsx
Original file line number Diff line number Diff line change
@@ -1,23 +1,94 @@
import { describe, expect, test } from 'vitest';
import { store } from '../src';

// !CURRENTLY NOT WORKING. easy solution is to just use store({items: []}) instead of store([]) for arrays.

describe('should work with array state', () => {
const listStore = store([1, 2, 3]);
test('get initial state', () => {
const listStore = store([1, 2, 3]);
const values = listStore.get();
expect(values).toStrictEqual([1, 2, 3]);
});

test('get', () => {
const counterValues = listStore.get();
test('set new array', () => {
const listStore = store([1, 2, 3]);
listStore.set([4, 5, 6]);
expect(listStore.get()).toStrictEqual([4, 5, 6]);
});

test('set new array with different length', () => {
const listStore = store([1, 2, 3]);
listStore.set([4, 5]);
expect(listStore.get()).toStrictEqual([4, 5]);
});

expect(counterValues).toStrictEqual([1, 2, 3]);
test('set empty array', () => {
const listStore = store([1, 2, 3]);
listStore.set([]);
expect(listStore.get()).toStrictEqual([]);
});

test('set', () => {
test('push item to array', () => {
const listStore = store([1, 2, 3]);
listStore.set((draft) => {
draft.push(4);
});
expect(listStore.get()).toStrictEqual([1, 2, 3, 4]);
});

// if fixed then remove the not from the below line
expect(listStore.get()).not.toStrictEqual([1, 2, 3, 4]);
test('pop item from array', () => {
const listStore = store([1, 2, 3]);
listStore.set((draft) => {
draft.pop();
});
expect(listStore.get()).toStrictEqual([1, 2]);
});

test('shift item from array', () => {
const listStore = store([1, 2, 3]);
listStore.set((draft) => {
draft.shift();
});
expect(listStore.get()).toStrictEqual([2, 3]);
});

test('unshift item to array', () => {
const listStore = store([1, 2, 3]);
listStore.set((draft) => {
draft.unshift(0);
});
expect(listStore.get()).toStrictEqual([0, 1, 2, 3]);
});

test('splice items from array', () => {
const listStore = store([1, 2, 3, 4, 5]);
listStore.set((draft) => {
draft.splice(1, 2);
});
expect(listStore.get()).toStrictEqual([1, 4, 5]);
});

test('splice and insert items to array', () => {
const listStore = store([1, 2, 3, 4, 5] as (number | string)[]);
listStore.set((draft) => {
draft.splice(1, 2, 'a', 'b');
});
expect(listStore.get()).toStrictEqual([1, 'a', 'b', 4, 5]);
});

test('update item in array', () => {
const listStore = store([1, 2, 3] as (number | string)[]);
listStore.set((draft) => {
draft[1] = 'x';
});
expect(listStore.get()).toStrictEqual([1, 'x', 3]);
});

test('clear array', () => {
const listStore = store([1, 2, 3]);
listStore.set((draft) => {
draft.length = 0;
});
expect(listStore.get()).toStrictEqual([]);
});

// Add more test cases as needed
});
123 changes: 123 additions & 0 deletions packages/store/test/v2/array-todo-store.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
import { describe, expect, test } from 'vitest';
import { store } from '../../src';
type Todo = {
id: number;
text: string;
completed: boolean;
};

const createTodoStore = () => {
const $todos = store<Todo[]>([]);

function addTodo(todo: Todo) {
$todos.set((draft) => {
draft.push(todo);
});
}

function toggleTodo(id: number) {
$todos.set((draft) => {
const todo = draft.find((todo) => todo.id === id);
if (todo) {
todo.completed = !todo.completed;
}
});
}

function removeTodo(id: number) {
$todos.set((draft) => {
const index = draft.findIndex((todo) => todo.id === id);
if (index !== -1) {
draft.splice(index, 1);
}
});
}

return {
$todos,
addTodo,
toggleTodo,
removeTodo,
};
};

describe('todo store', () => {
test('add todo item', () => {
const { $todos, addTodo } = createTodoStore();
addTodo({ id: 1, text: 'Buy groceries', completed: false });
expect($todos.get()).toStrictEqual([
{ id: 1, text: 'Buy groceries', completed: false },
]);
});

test('add multiple todo items', () => {
const { $todos, addTodo } = createTodoStore();
addTodo({ id: 1, text: 'Buy groceries', completed: false });
addTodo({ id: 2, text: 'Do laundry', completed: false });
expect($todos.get()).toStrictEqual([
{ id: 1, text: 'Buy groceries', completed: false },
{ id: 2, text: 'Do laundry', completed: false },
]);
});

test('toggle todo item', () => {
const { $todos, addTodo, toggleTodo } = createTodoStore();
addTodo({ id: 1, text: 'Buy groceries', completed: false });
toggleTodo(1);
expect($todos.get()).toStrictEqual([
{ id: 1, text: 'Buy groceries', completed: true },
]);
});

test('toggle non-existent todo item', () => {
const { $todos, addTodo, toggleTodo } = createTodoStore();
addTodo({ id: 1, text: 'Buy groceries', completed: false });
toggleTodo(2);
expect($todos.get()).toStrictEqual([
{ id: 1, text: 'Buy groceries', completed: false },
]);
});

test('remove todo item', () => {
const { $todos, addTodo, removeTodo } = createTodoStore();
addTodo({ id: 1, text: 'Buy groceries', completed: false });
addTodo({ id: 2, text: 'Do laundry', completed: false });
removeTodo(1);
expect($todos.get()).toStrictEqual([
{ id: 2, text: 'Do laundry', completed: false },
]);
});

test('remove non-existent todo item', () => {
const { $todos, addTodo, removeTodo } = createTodoStore();
addTodo({ id: 1, text: 'Buy groceries', completed: false });
removeTodo(2);
expect($todos.get()).toStrictEqual([
{ id: 1, text: 'Buy groceries', completed: false },
]);
});

test('update todo item text', () => {
const { $todos, addTodo } = createTodoStore();
addTodo({ id: 1, text: 'Buy groceries', completed: false });
$todos.set((draft) => {
const todo = draft.find((todo) => todo.id === 1);
if (todo) {
todo.text = 'Buy milk';
}
});
expect($todos.get()).toStrictEqual([
{ id: 1, text: 'Buy milk', completed: false },
]);
});

test('clear all todo items', () => {
const { $todos, addTodo } = createTodoStore();
addTodo({ id: 1, text: 'Buy groceries', completed: false });
addTodo({ id: 2, text: 'Do laundry', completed: false });
$todos.set([]);
expect($todos.get()).toStrictEqual([]);
});

// Add more test cases as needed
});
19 changes: 19 additions & 0 deletions packages/store/test/v2/state.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,23 @@ describe('store', () => {
const count = state({ hello: 'world' });
expect(count.get()).toEqual({ hello: 'world' });
});

test('array - empty', () => {
const count = state([]);
expect(count.get()).toEqual([]);
});

test('array - non-empty', () => {
const count = state([1, 2, 3]);
expect(count.get()).toEqual([1, 2, 3]);
});

test('array - after changes', () => {
const count = state([1, 2, 3]);
expect(count.get()).toEqual([1, 2, 3]);
count.set([4, 5, 6]);
expect(count.get()).toEqual([4, 5, 6]);
});
});

const createCountStore = ({ id }: { id: string }) => {
Expand All @@ -107,6 +124,8 @@ describe('store', () => {
};
};



// type TempType = State<number>;

describe('basic methods', () => {
Expand Down

0 comments on commit 960ba51

Please sign in to comment.