Skip to content

Instantly share code, notes, and snippets.

@elliotlarson
Created July 22, 2018 03:41
Show Gist options
  • Save elliotlarson/0c21496b8ebd7c4fe4248f17aec95c0b to your computer and use it in GitHub Desktop.
Save elliotlarson/0c21496b8ebd7c4fe4248f17aec95c0b to your computer and use it in GitHub Desktop.
testing Redux characters file
import { reducer, actions } from "./characters";
it("creates a character", () => {
const newCharacter = { id: 42, firstName: "Jeffrey", lastName: "Lebowski" };
const action = actions.create(newCharacter);
const state = reducer(undefined, action);
expect(state.byId[42]).toEqual(newCharacter);
expect(state.allIds).toEqual([42]);
});
it("creates a new character and assigns a UUID", () => {
const newCharacter = { firstName: "Jeffrey", lastName: "Lebowski" };
const action = actions.create(newCharacter);
const state = reducer(undefined, action);
expect(state.allIds).toHaveLength(1);
const newId = state.allIds[0];
expect(newId).toMatch(
/[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}/
);
expect(state.byId[newId]).toMatchObject({ ...newCharacter, id: newId });
});
it("updates a character", () => {
const initState = {
byId: {
42: { id: 42, firstName: "Jeffrey", lastName: "Lebowski" }
},
allIds: [42]
};
const updatedCharacter = { id: 42, firstName: "The", lastName: "Dude" };
const action = actions.update(updatedCharacter);
const state = reducer(initState, action);
expect(state.byId[42]).toEqual(updatedCharacter);
});
it("deletes a character", () => {
const initState = {
byId: {
42: { id: 42, firstName: "Jeffrey", lastName: "Lebowski" }
},
allIds: [42]
};
const action = actions.delete(42);
const state = reducer(initState, action);
expect(state.byId[42]).toBeUndefined();
expect(state.allIds).toEqual([]);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment