-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
50e0071
commit a2cb3d5
Showing
2 changed files
with
118 additions
and
4 deletions.
There are no files selected for viewing
41 changes: 41 additions & 0 deletions
41
src/app/shared/state/__snapshots__/books.reducer.spec.ts.snap
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,41 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`Books Reducer should add a newly created book to the state 1`] = ` | ||
Object { | ||
"activeBookId": "1", | ||
"entities": Object { | ||
"1": Object { | ||
"earnings": 200000000, | ||
"id": "1", | ||
"name": "Forrest Gump", | ||
}, | ||
}, | ||
"ids": Array [ | ||
"1", | ||
], | ||
} | ||
`; | ||
|
||
exports[`Books Reducer should load all books when the API loads them all successfully 1`] = ` | ||
Object { | ||
"activeBookId": null, | ||
"entities": Object { | ||
"1": Object { | ||
"earnings": 1000000, | ||
"id": "1", | ||
"name": "Castaway", | ||
}, | ||
}, | ||
"ids": Array [ | ||
"1", | ||
], | ||
} | ||
`; | ||
|
||
exports[`Books Reducer should remove books from the state when they are deleted 1`] = ` | ||
Object { | ||
"activeBookId": null, | ||
"entities": Object {}, | ||
"ids": Array [], | ||
} | ||
`; |
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,7 +1,80 @@ | ||
// import { BooksApiActions, BooksPageActions } from "src/app/books/actions"; | ||
// import { Book } from "../models/book.model"; | ||
// import { reducer, initialState } from "./book.reducer"; | ||
import { BooksApiActions, BooksPageActions } from "src/app/books/actions"; | ||
import { Book } from "../models/book.model"; | ||
import { | ||
reducer, | ||
initialState, | ||
selectActiveBook, | ||
adapter, | ||
selectAll | ||
} from "./books.reducer"; | ||
|
||
describe("Books Reducer", () => { | ||
it("should return the initial state when initialized", () => {}); | ||
it("should return the initial state when initialized", () => { | ||
const state = reducer(undefined, { type: "@@init" } as any); | ||
|
||
expect(state).toBe(initialState); | ||
}); | ||
|
||
it("should load all books when the API loads them all successfully", () => { | ||
const books: Book[] = [{ id: "1", name: "Castaway", earnings: 1000000 }]; | ||
const action = BooksApiActions.booksLoaded({ books }); | ||
|
||
const state = reducer(initialState, action); | ||
|
||
expect(state).toMatchSnapshot(); | ||
}); | ||
|
||
it("should add a newly created book to the state", () => { | ||
const book: Book = { id: "1", name: "Forrest Gump", earnings: 200000000 }; | ||
const action = BooksApiActions.bookCreated({ book }); | ||
|
||
const state = reducer(initialState, action); | ||
|
||
expect(state).toMatchSnapshot(); | ||
}); | ||
|
||
it("should remove books from the state when they are deleted", () => { | ||
const book: Book = { id: "1", name: "Apollo 13", earnings: 1000 }; | ||
const firstAction = BooksApiActions.bookCreated({ book }); | ||
const secondAction = BooksApiActions.bookDeleted({ book }); | ||
|
||
const state = [firstAction, secondAction].reduce(reducer, initialState); | ||
|
||
expect(state).toMatchSnapshot(); | ||
}); | ||
|
||
describe("Selectors", () => { | ||
const initialState = { activeBookId: null, ids: [], entities: {} }; | ||
|
||
describe("selectActiveBook", () => { | ||
it("should return null if there is no active book", () => { | ||
const result = selectActiveBook(initialState); | ||
|
||
expect(result).toBe(null); | ||
}); | ||
|
||
it("should return the active book if there is one", () => { | ||
const book: Book = { id: "1", name: "Castaway", earnings: 1000000 }; | ||
const state = adapter.addAll([book], { | ||
...initialState, | ||
activeBookId: "1" | ||
}); | ||
const result = selectActiveBook(state); | ||
|
||
expect(result).toBe(book); | ||
}); | ||
}); | ||
|
||
describe("selectAll", () => { | ||
it("should return all the loaded books", () => { | ||
const books: Book[] = [ | ||
{ id: "1", name: "Castaway", earnings: 1000000 } | ||
]; | ||
const state = adapter.addAll(books, initialState); | ||
const result = selectAll(state); | ||
|
||
expect(result.length).toBe(1); | ||
}); | ||
}); | ||
}); | ||
}); |