|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447 |
- /* eslint-env mocha */
- import expect from "expect"
- import System from "core/system"
- import { fromJS } from "immutable"
-
- describe("bound system", function(){
-
- describe("wrapActions", function(){
-
- it("should replace an action", function(){
- // Given
- const system = new System({
- plugins: {
- statePlugins: {
- josh: {
- actions: {
- simple: () => {
- return { type: "simple" }
- }
- },
- wrapActions: {
- simple: () => () => {
- return { type: "newSimple" }
- }
- }
- }
- }
- }
- })
-
- // When
- var action = system.getSystem().joshActions.simple(1)
- expect(action).toEqual({
- type: "newSimple"
- })
-
- })
-
- it("should expose the original action, and the system as args", function(){
- // Given
- const simple = () => ({type: "simple" })
- const system = new System({
- plugins: {
- statePlugins: {
- josh: {
- actions: { simple },
- wrapActions: {
- simple: (oriAction, system) => (actionArg) => {
- return {
- type: "newSimple",
- oriActionResult: oriAction(),
- system: system.getSystem(),
- actionArg
- }
- }
- }
- }
- }
- }
- })
-
- // When
- var action = system.getSystem().joshActions.simple(1)
- expect(action).toEqual({
- type: "newSimple",
- oriActionResult: { type: "simple" },
- system: system.getSystem(),
- actionArg: 1
- })
-
- })
-
- it("should support multiple wraps of the same action", function(){
- const system = new System({
- plugins: [
- {
- statePlugins: {
- kyle: {
- actions: {
- simple: () => {
- return {
- type: "simple",
- }
- }
- }
- }
- }
- },
- {
- statePlugins: {
- kyle: {
- wrapActions: {
- simple: (ori) => () => {
- return {
- ...ori(),
- firstWrap: true
- }
- }
- }
- }
- }
- },
- {
- statePlugins: {
- kyle: {
- wrapActions: {
- simple: (ori) => () => {
- return {
- ...ori(),
- secondWrap: true
- }
- }
- }
- }
- }
- }
- ]
- })
-
- // When
- var action = system.getSystem().kyleActions.simple(1)
- expect(action).toEqual({
- type: "simple",
- firstWrap: true,
- secondWrap: true,
- })
-
- })
-
- it("should execute wrapActions in the order they appear ( via plugins )", function(){
- const system = new System({
- plugins: [
- {
- statePlugins: {
- kyle: {
- actions: {
- simple: () => {
- return {
- type: "one",
- }
- }
- }
- }
- }
- },
- {
- statePlugins: {
- kyle: {
- wrapActions: {
- simple: (ori) => () => {
- const obj = ori()
- obj.type += "-two"
- return obj
- }
- }
- }
- }
- },
- {
- statePlugins: {
- kyle: {
- wrapActions: {
- simple: (ori) => () => {
- const obj = ori()
- obj.type += "-three"
- return obj
- }
- }
- }
- }
- }
- ]
- })
-
- // When
- var action = system.getSystem().kyleActions.simple(1)
- expect(action.type).toEqual("one-two-three")
-
- })
-
- it("should have a the latest system", function(){
- // Given
- const system = new System({
- plugins: [
- {
- statePlugins: {
- kyle: {
- actions: {
- simple: () => {
- return {
- type: "one",
- }
- }
- },
- wrapActions: {
- simple: (ori, {joshActions}) => () => {
- return joshActions.hello()
- }
- }
- }
- }
- },
- ]
- })
-
- // When
- const kyleActions = system.getSystem().kyleActions
-
- system.register({
- statePlugins: {
- josh: {
- actions: {
- hello(){ return {type: "hello" } }
- }
- }
- }
- })
-
- const action = kyleActions.simple()
- expect(action).toEqual({ type: "hello"})
- })
-
- it.skip("should be able to create async actions", function(){
- const system = new System({
- plugins: [
- {
- statePlugins: {
- kyle: {
- actions: {
- simple: () => {
- return {
- type: "one",
- }
- }
- }
- }
- }
- },
- {
- statePlugins: {
- kyle: {
- wrapActions: {
- // eslint-disable-next-line no-unused-vars
- simple: (ori) => (arg) => (sys) => {
- return { type: "called" }
- }
- }
- }
- }
- },
- ]
- })
-
- // When
- var action = system.getSystem().kyleActions.simple(1)
- expect(action.type).toEqual("called")
-
- })
-
-
- })
-
- describe("selectors", function(){
-
- it("should have the first arg be the nested state, and all other args to follow", function(){
-
- // Given
- const system = new System({
- state: {
- josh: {
- one: 1
- }
- },
- plugins: {
- statePlugins: {
- josh: {
- selectors: {
- simple: (state, arg1) => {
- return { state, arg1 }
- }
- }
- }
- }
- }
-
- })
-
- // When
- var res = system.getSystem().joshSelectors.simple(1)
- expect(res).toEqual({
- state: fromJS({
- one: 1
- }),
- arg1: 1
- })
-
- })
-
- describe("when selector returns a funtcion", function(){
-
- it("should pass the system to that function", function(){
-
- // Given
- const system = new System({
- plugins: {
- statePlugins: {
- josh: {
- selectors: {
- advanced: () => (mySystem) => {
- // Then
- expect(mySystem).toEqual(system.getSystem())
- return "hi"
- }
- }
- }
- }
- }
-
- })
-
- // When
- var res = system.getSystem().joshSelectors.advanced(1)
- expect(res).toEqual("hi")
-
- })
-
- })
-
- describe("wrapSelectors", () => {
- it("should wrap a selector and provide a reference to the original", function(){
-
- // Given
- const system = new System({
- plugins: [
- {
- statePlugins: {
- doge: {
- selectors: {
- wow: () => (system) => {
- return "original"
- }
- }
- }
- }
- },
- {
- statePlugins: {
- doge: {
- wrapSelectors: {
- wow: (ori) => (system) => {
- // Then
- return ori() + " wrapper"
- }
- }
- }
- }
- }
- ]
- })
-
- // When
- var res = system.getSystem().dogeSelectors.wow(1)
- expect(res).toEqual("original wrapper")
-
- })
-
- it("should provide a live reference to the system to a wrapper", function(done){
-
- // Given
- const mySystem = new System({
- plugins: [
- {
- statePlugins: {
- doge: {
- selectors: {
- wow: () => (system) => {
- return "original"
- }
- }
- }
- }
- },
- {
- statePlugins: {
- doge: {
- wrapSelectors: {
- wow: (ori, system) => () => {
- // Then
- expect(mySystem.getSystem()).toEqual(system.getSystem())
- done()
- return ori() + " wrapper"
- }
- }
- }
- }
- }
- ]
- })
-
- mySystem.getSystem().dogeSelectors.wow(1)
- })
-
- it("should provide the state as the first argument to the inner function", function(done){
-
- // Given
- const mySystem = new System({
- state: {
- doge: {
- abc: "123"
- }
- },
- plugins: [
- {
- statePlugins: {
- doge: {
- selectors: {
- wow: () => (system) => {
- return "original"
- }
- }
- }
- }
- },
- {
- statePlugins: {
- doge: {
- wrapSelectors: {
- wow: (ori, system) => (dogeState) => {
- // Then
- expect(dogeState.toJS().abc).toEqual("123")
- done()
- return ori() + " wrapper"
- }
- }
- }
- }
- }
- ]
- })
-
- mySystem.getSystem().dogeSelectors.wow(1)
- })
- })
-
- })
-
- })
|