From c3968f2ae97b2c61a7a87b1481bd46ab663f6a62 Mon Sep 17 00:00:00 2001 From: Vildan Softic Date: Wed, 3 Oct 2018 23:09:58 +0200 Subject: [PATCH] test(decorator): multiselector slice changed handler related issue https://github.com/aurelia/store/issues/68 --- test/unit/decorator.spec.ts | 30 +++++++++++++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/test/unit/decorator.spec.ts b/test/unit/decorator.spec.ts index 02038f3..9eb0197 100644 --- a/test/unit/decorator.spec.ts +++ b/test/unit/decorator.spec.ts @@ -1,6 +1,6 @@ import { Container } from "aurelia-framework"; import { Subscription } from "rxjs"; -import { pluck } from "rxjs/operators"; +import { pluck, distinctUntilChanged } from "rxjs/operators"; import { Store } from "../../src/store"; import { connectTo } from "../../src/decorator"; @@ -627,6 +627,34 @@ describe("using decorators", () => { expect(sut.fooChanged).toHaveBeenCalledWith("targetProp", initialState, "foobar"); }); + it("should call changed handler for multiple selectors only when their state slice is affected", async () => { + const { store } = arrange(); + const changeOnlyBar = (state: DemoState) => Object.assign({}, state, { bar: "changed" }); + store.registerAction("changeOnlyBar", changeOnlyBar); + + @connectTo({ + selector: { + foo: (store) => store.state.pipe(pluck("foo"), distinctUntilChanged()), + bar: (store) => store.state.pipe(pluck("bar"), distinctUntilChanged()) + } + }) + class DemoStoreConsumer { + barChanged() { } + + fooChanged() { } + } + + const sut = new DemoStoreConsumer() as Spied; + const spyFoo = jest.spyOn(sut, "fooChanged"); + const spyBar = jest.spyOn(sut, "barChanged"); + (sut as any).bind(); + + await store.dispatch(changeOnlyBar); + + expect(spyFoo).toHaveBeenCalledTimes(1); + expect(spyBar).toHaveBeenCalledTimes(2); + }); + it("should check whether the method exists before calling it and throw a meaningful error", () => { arrange();