-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test(store): add extensive tests for array strore methods (root level…
… array)
- Loading branch information
1 parent
e0132ab
commit 960ba51
Showing
3 changed files
with
222 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters