Skip to content

Commit

Permalink
fix(logEffects): Removed log effects helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
iskandersierra committed Oct 22, 2016
1 parent 601bbac commit d55cec9
Show file tree
Hide file tree
Showing 2 changed files with 1 addition and 94 deletions.
80 changes: 1 addition & 79 deletions src/createEffects.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,85 +20,7 @@ import {
} from "./interfaces";
import { createStore, createStoreExtensions } from "./createStore";
import extendWith from "./extendWith";
import {
logUpdatesEffect, logUpdatesByActionTypeEffect,
createEffects,
} from "./createEffects";

describe("logUpdatesEffect", () => {
it("it should be a function",
() => expect(typeof logUpdatesEffect).toBe("function"));
describe("When it is called with a captioner", () => {
const captioner = jest.fn(() => "***CAPTION***");
const captionedEffect = logUpdatesEffect(captioner);
it("The result should be a function",
() => expect(typeof captionedEffect).toBe("function"));
it("The captioner should not be called",
() => expect(captioner).not.toBeCalled());
describe("When it is called with a logger", () => {
const logger = jest.fn();
const loggerEffect = captionedEffect(logger);
it("The result should be a function",
() => expect(typeof loggerEffect).toBe("function"));
it("The captioner should not be called",
() => expect(captioner).not.toBeCalled());
it("The logger should not be called",
() => expect(logger).not.toBeCalled());
describe("When a store gets the effect applied", () => {
const reducer = jest.fn((s, a) => s);
const state = { title: "hello" };
const store = createStore(reducer, state);
const update = { action: { type: "A" }, state };
const loggerPromise = loggerEffect(store)
.take(1).first()
.toPromise() as PromiseLike<StateUpdate<{ title: string }>>;
it("captioner and logger should have been called once",
() => {
store.dispatch(update.action);
return loggerPromise.then(up => {
expect(captioner).toHaveBeenCalledTimes(1);
expect(logger).toHaveBeenCalledTimes(1);
expect(captioner).toBeCalledWith(update, store);
expect(logger).toBeCalledWith("***CAPTION***", update);
expect(up).toEqual(update);
});
});

}); // describe When a store gets the effect applied
}); // describe When it is called with a logger
}); // describe When it is called with a captioner
}); // describe logUpdatesEffect

describe("logUpdatesByActionTypeEffect", () => {
describe("When it is called with a logger", () => {
const logger = jest.fn();
const loggerEffect = logUpdatesByActionTypeEffect(logger);
it("The result should be a function",
() => expect(typeof loggerEffect).toBe("function"));
it("The logger should not be called",
() => expect(logger).not.toBeCalled());
describe("When a store gets the effect applied", () => {
const reducer = jest.fn((s, a) => s);
const state = { title: "hello" };
const store = createStore(reducer, state,
extendWith(() => ({ caption: "MyStore" }))
);
const update = { action: { type: "A" }, state };
const loggerPromise = loggerEffect(store)
.take(1).first()
.toPromise() as PromiseLike<StateUpdate<{ title: string }>>;
it("logger should have been called once",
() => {
store.dispatch(update.action);
return loggerPromise.then(up => {
expect(logger).toHaveBeenCalledTimes(1);
expect(logger).toBeCalledWith("MyStore: ON A", update);
expect(up).toEqual(update);
});
});
}); // describe When a store gets the effect applied
}); // describe When it is called with a logger
}); // describe logUpdatesByActionTypeEffect
import { createEffects } from "./createEffects";

describe("createEffects", () => {
describe("Given a simple store", () => {
Expand Down
15 changes: 0 additions & 15 deletions src/createEffects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,6 @@ import {

const scheduler = queue;

export const logUpdatesEffect =
(captioner: (update: StateUpdate<any>, store: Store<any>) => any) =>
(logger: (message?: any, ...parameters: any[]) => void) =>
(store: Store<any>) => {
const effect = (up: StateUpdate<any>) => logger(captioner(up, store), up);
return store.update$.do(effect);
};

export const logUpdatesByActionTypeEffect =
logUpdatesEffect((up, store) => {
let caption = (store as any).caption;
if (caption) { caption += ": "; } else { caption = ""; }
return caption + "ON " + up.action.type;
});

export const createEffects =
(dispatch: Dispatcher, ...effects: Effect[]): void =>
effects.forEach(e => e.subscribeOn(scheduler).subscribe(dispatch));
Expand Down

0 comments on commit d55cec9

Please sign in to comment.