Skip to content

Commit a011ea5

Browse files
committed
Add comment slice tests
1 parent 60068d6 commit a011ea5

File tree

2 files changed

+83
-1
lines changed

2 files changed

+83
-1
lines changed
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
import { nanoid } from "@reduxjs/toolkit"
2+
import type { Comment } from "./commentSlice"
3+
import {
4+
commentSlice,
5+
addComment,
6+
deleteComment,
7+
selectAllComments,
8+
selectCommentById,
9+
selectCommentTotal,
10+
selectCommentsByTodoId,
11+
commentAdapter,
12+
} from "./commentSlice"
13+
import type { AppStore } from "../../app/store"
14+
import { makeStore } from "../../app/store"
15+
import { todoAdapter, type Todo, deleteTodo } from "./todoSlice"
16+
17+
interface LocalTestContext {
18+
store: AppStore
19+
}
20+
21+
const initialTodo: Todo = { id: nanoid(), title: "Initial todo" }
22+
const initialComment: Comment = {
23+
id: nanoid(),
24+
todoId: initialTodo.id,
25+
message: "Initial comment",
26+
}
27+
28+
describe<LocalTestContext>("comment slice", it => {
29+
beforeEach<LocalTestContext>(context => {
30+
const store = makeStore({
31+
todo: todoAdapter.setOne(todoAdapter.getInitialState(), initialTodo),
32+
comments: commentAdapter.setOne(
33+
commentAdapter.getInitialState(),
34+
initialComment,
35+
),
36+
})
37+
38+
context.store = store
39+
})
40+
41+
it("should handle initial state", () => {
42+
expect(commentSlice.reducer(undefined, { type: "unknown" })).toStrictEqual(
43+
commentSlice.getInitialState(),
44+
)
45+
})
46+
47+
it("should add a comment", ({ store }) => {
48+
expect(selectCommentTotal(store.getState())).toBe(1)
49+
expect(
50+
selectCommentById(store.getState(), initialComment.id),
51+
).toStrictEqual(initialComment)
52+
const comment = {
53+
todoId: initialTodo.id,
54+
message: "This is a comment",
55+
}
56+
57+
store.dispatch(addComment(comment))
58+
59+
const comments = selectAllComments(store.getState())
60+
expect(comments).toEqual([
61+
initialComment,
62+
{ id: expect.any(String), ...comment },
63+
])
64+
})
65+
66+
it("should delete a comment", ({ store }) => {
67+
expect(selectCommentTotal(store.getState())).toBe(1)
68+
store.dispatch(deleteComment(initialComment.id))
69+
70+
const comments = selectAllComments(store.getState())
71+
expect(comments.length).toBe(0)
72+
})
73+
74+
it("should delete a comment when its todo is deleted", ({ store }) => {
75+
expect(selectCommentTotal(store.getState())).toBe(1)
76+
expect(selectCommentsByTodoId(store.getState(), initialTodo.id)).toEqual([
77+
initialComment,
78+
])
79+
store.dispatch(deleteTodo(initialTodo.id))
80+
expect(selectCommentsByTodoId(store.getState(), initialTodo.id)).toEqual([])
81+
})
82+
})

examples/lazy-injection/kitchen-sink/src/features/todos/commentSlice.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ export interface Comment {
99
message: string
1010
}
1111

12-
const commentAdapter = createEntityAdapter<Comment>()
12+
export const commentAdapter = createEntityAdapter<Comment>()
1313

1414
const localisedSelectors = commentAdapter.getSelectors()
1515

0 commit comments

Comments
 (0)