@reduxjs/toolkit#Store TypeScript Examples

The following examples show how to use @reduxjs/toolkit#Store. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: index.test.tsx    From react-boilerplate-cra-template with MIT License 6 votes vote down vote up
renderGithubRepoForm = (store: Store) =>
  render(
    <Provider store={store}>
      <ThemeProvider>
        <HelmetProvider>
          <GithubRepoForm />
        </HelmetProvider>
      </ThemeProvider>
    </Provider>,
  )
Example #2
Source File: ThemeProvider.test.tsx    From oasis-wallet-web with Apache License 2.0 5 votes vote down vote up
renderThemeProvider = (store: Store, Child: React.FunctionComponent) =>
  render(
    <Provider store={store}>
      <ThemeProvider>
        <Child />
      </ThemeProvider>
    </Provider>,
  )
Example #3
Source File: index.test.tsx    From react-boilerplate-cra-template with MIT License 5 votes vote down vote up
renderThemeSwitch = (store: Store) =>
  render(
    <Provider store={store}>
      <ThemeProvider>
        <ThemeSwitch />
      </ThemeProvider>
    </Provider>,
  )
Example #4
Source File: ThemeProvider.test.tsx    From react-boilerplate-cra-template with MIT License 5 votes vote down vote up
renderThemeProvider = (store: Store, Child: React.FunctionComponent) =>
  render(
    <Provider store={store}>
      <ThemeProvider>
        <Child />
      </ThemeProvider>
    </Provider>,
  )
Example #5
Source File: ThemeProvider.test.tsx    From datart with Apache License 2.0 5 votes vote down vote up
renderThemeProvider = (store: Store, Child: React.FunctionComponent) =>
  render(
    <Provider store={store}>
      <ThemeProvider>
        <Child />
      </ThemeProvider>
    </Provider>,
  )
Example #6
Source File: store.ts    From pali-wallet with MIT License 5 votes vote down vote up
store: Store<{
  price: IPriceState;
  vault: IVaultState;
}> = configureStore({
  reducer: persistedReducer,
  middleware,
  devTools: nodeEnv !== 'production' && nodeEnv !== 'test',
})
Example #7
Source File: reducer.test.ts    From cuiswap with GNU General Public License v3.0 4 votes vote down vote up
describe('multicall reducer', () => {
  let store: Store<MulticallState>
  beforeEach(() => {
    store = createStore(reducer)
  })

  it('has correct initial state', () => {
    expect(store.getState().callResults).toEqual({})
    expect(store.getState().callListeners).toEqual(undefined)
  })

  describe('addMulticallListeners', () => {
    it('adds listeners', () => {
      store.dispatch(
        addMulticallListeners({
          chainId: 1,
          calls: [
            {
              address: DAI_ADDRESS,
              callData: '0x'
            }
          ]
        })
      )
      expect(store.getState()).toEqual({
        callListeners: {
          [1]: {
            [`${DAI_ADDRESS}-0x`]: {
              [1]: 1
            }
          }
        },
        callResults: {}
      })
    })
  })

  describe('removeMulticallListeners', () => {
    it('noop', () => {
      store.dispatch(
        removeMulticallListeners({
          calls: [
            {
              address: DAI_ADDRESS,
              callData: '0x'
            }
          ],
          chainId: 1
        })
      )
      expect(store.getState()).toEqual({ callResults: {}, callListeners: {} })
    })
    it('removes listeners', () => {
      store.dispatch(
        addMulticallListeners({
          chainId: 1,
          calls: [
            {
              address: DAI_ADDRESS,
              callData: '0x'
            }
          ]
        })
      )
      store.dispatch(
        removeMulticallListeners({
          calls: [
            {
              address: DAI_ADDRESS,
              callData: '0x'
            }
          ],
          chainId: 1
        })
      )
      expect(store.getState()).toEqual({
        callResults: {},
        callListeners: { [1]: { [`${DAI_ADDRESS}-0x`]: {} } }
      })
    })
  })

  describe('updateMulticallResults', () => {
    it('updates data if not present', () => {
      store.dispatch(
        updateMulticallResults({
          chainId: 1,
          blockNumber: 1,
          results: {
            abc: '0x'
          }
        })
      )
      expect(store.getState()).toEqual({
        callResults: {
          [1]: {
            abc: {
              blockNumber: 1,
              data: '0x'
            }
          }
        }
      })
    })
    it('updates old data', () => {
      store.dispatch(
        updateMulticallResults({
          chainId: 1,
          blockNumber: 1,
          results: {
            abc: '0x'
          }
        })
      )
      store.dispatch(
        updateMulticallResults({
          chainId: 1,
          blockNumber: 2,
          results: {
            abc: '0x2'
          }
        })
      )
      expect(store.getState()).toEqual({
        callResults: {
          [1]: {
            abc: {
              blockNumber: 2,
              data: '0x2'
            }
          }
        }
      })
    })
    it('ignores late updates', () => {
      store.dispatch(
        updateMulticallResults({
          chainId: 1,
          blockNumber: 2,
          results: {
            abc: '0x2'
          }
        })
      )
      store.dispatch(
        updateMulticallResults({
          chainId: 1,
          blockNumber: 1,
          results: {
            abc: '0x1'
          }
        })
      )
      expect(store.getState()).toEqual({
        callResults: {
          [1]: {
            abc: {
              blockNumber: 2,
              data: '0x2'
            }
          }
        }
      })
    })
  })
  describe('fetchingMulticallResults', () => {
    it('updates state to fetching', () => {
      store.dispatch(
        fetchingMulticallResults({
          chainId: 1,
          fetchingBlockNumber: 2,
          calls: [{ address: DAI_ADDRESS, callData: '0x0' }]
        })
      )
      expect(store.getState()).toEqual({
        callResults: {
          [1]: {
            [`${DAI_ADDRESS}-0x0`]: { fetchingBlockNumber: 2 }
          }
        }
      })
    })

    it('updates state to fetching even if already fetching older block', () => {
      store.dispatch(
        fetchingMulticallResults({
          chainId: 1,
          fetchingBlockNumber: 2,
          calls: [{ address: DAI_ADDRESS, callData: '0x0' }]
        })
      )
      store.dispatch(
        fetchingMulticallResults({
          chainId: 1,
          fetchingBlockNumber: 3,
          calls: [{ address: DAI_ADDRESS, callData: '0x0' }]
        })
      )
      expect(store.getState()).toEqual({
        callResults: {
          [1]: {
            [`${DAI_ADDRESS}-0x0`]: { fetchingBlockNumber: 3 }
          }
        }
      })
    })

    it('does not do update if fetching newer block', () => {
      store.dispatch(
        fetchingMulticallResults({
          chainId: 1,
          fetchingBlockNumber: 2,
          calls: [{ address: DAI_ADDRESS, callData: '0x0' }]
        })
      )
      store.dispatch(
        fetchingMulticallResults({
          chainId: 1,
          fetchingBlockNumber: 1,
          calls: [{ address: DAI_ADDRESS, callData: '0x0' }]
        })
      )
      expect(store.getState()).toEqual({
        callResults: {
          [1]: {
            [`${DAI_ADDRESS}-0x0`]: { fetchingBlockNumber: 2 }
          }
        }
      })
    })
  })

  describe('errorFetchingMulticallResults', () => {
    it('does nothing if not fetching', () => {
      store.dispatch(
        errorFetchingMulticallResults({
          chainId: 1,
          fetchingBlockNumber: 1,
          calls: [{ address: DAI_ADDRESS, callData: '0x0' }]
        })
      )
      expect(store.getState()).toEqual({
        callResults: {
          [1]: {}
        }
      })
    })
    it('updates block number if we were fetching', () => {
      store.dispatch(
        fetchingMulticallResults({
          chainId: 1,
          fetchingBlockNumber: 2,
          calls: [{ address: DAI_ADDRESS, callData: '0x0' }]
        })
      )
      store.dispatch(
        errorFetchingMulticallResults({
          chainId: 1,
          fetchingBlockNumber: 2,
          calls: [{ address: DAI_ADDRESS, callData: '0x0' }]
        })
      )
      expect(store.getState()).toEqual({
        callResults: {
          [1]: {
            [`${DAI_ADDRESS}-0x0`]: {
              blockNumber: 2,
              // null data indicates error
              data: null
            }
          }
        }
      })
    })
    it('does nothing if not errored on latest block', () => {
      store.dispatch(
        fetchingMulticallResults({
          chainId: 1,
          fetchingBlockNumber: 3,
          calls: [{ address: DAI_ADDRESS, callData: '0x0' }]
        })
      )
      store.dispatch(
        errorFetchingMulticallResults({
          chainId: 1,
          fetchingBlockNumber: 2,
          calls: [{ address: DAI_ADDRESS, callData: '0x0' }]
        })
      )
      expect(store.getState()).toEqual({
        callResults: {
          [1]: {
            [`${DAI_ADDRESS}-0x0`]: { fetchingBlockNumber: 3 }
          }
        }
      })
    })
  })
})
Example #8
Source File: reducer.test.ts    From interface-v2 with GNU General Public License v3.0 4 votes vote down vote up
describe('multicall reducer', () => {
  let store: Store<MulticallState>;
  beforeEach(() => {
    store = createStore(reducer);
  });

  it('has correct initial state', () => {
    expect(store.getState().callResults).toEqual({});
    expect(store.getState().callListeners).toEqual(undefined);
  });

  describe('addMulticallListeners', () => {
    it('adds listeners', () => {
      store.dispatch(
        addMulticallListeners({
          chainId: 1,
          calls: [
            {
              address: DAI_ADDRESS,
              callData: '0x',
            },
          ],
        }),
      );
      expect(store.getState()).toEqual({
        callListeners: {
          [1]: {
            [`${DAI_ADDRESS}-0x`]: {
              [1]: 1,
            },
          },
        },
        callResults: {},
      });
    });
  });

  describe('removeMulticallListeners', () => {
    it('noop', () => {
      store.dispatch(
        removeMulticallListeners({
          calls: [
            {
              address: DAI_ADDRESS,
              callData: '0x',
            },
          ],
          chainId: 1,
        }),
      );
      expect(store.getState()).toEqual({ callResults: {}, callListeners: {} });
    });
    it('removes listeners', () => {
      store.dispatch(
        addMulticallListeners({
          chainId: 1,
          calls: [
            {
              address: DAI_ADDRESS,
              callData: '0x',
            },
          ],
        }),
      );
      store.dispatch(
        removeMulticallListeners({
          calls: [
            {
              address: DAI_ADDRESS,
              callData: '0x',
            },
          ],
          chainId: 1,
        }),
      );
      expect(store.getState()).toEqual({
        callResults: {},
        callListeners: { [1]: { [`${DAI_ADDRESS}-0x`]: {} } },
      });
    });
  });

  describe('updateMulticallResults', () => {
    it('updates data if not present', () => {
      store.dispatch(
        updateMulticallResults({
          chainId: 1,
          blockNumber: 1,
          results: {
            abc: '0x',
          },
        }),
      );
      expect(store.getState()).toEqual({
        callResults: {
          [1]: {
            abc: {
              blockNumber: 1,
              data: '0x',
            },
          },
        },
      });
    });
    it('updates old data', () => {
      store.dispatch(
        updateMulticallResults({
          chainId: 1,
          blockNumber: 1,
          results: {
            abc: '0x',
          },
        }),
      );
      store.dispatch(
        updateMulticallResults({
          chainId: 1,
          blockNumber: 2,
          results: {
            abc: '0x2',
          },
        }),
      );
      expect(store.getState()).toEqual({
        callResults: {
          [1]: {
            abc: {
              blockNumber: 2,
              data: '0x2',
            },
          },
        },
      });
    });
    it('ignores late updates', () => {
      store.dispatch(
        updateMulticallResults({
          chainId: 1,
          blockNumber: 2,
          results: {
            abc: '0x2',
          },
        }),
      );
      store.dispatch(
        updateMulticallResults({
          chainId: 1,
          blockNumber: 1,
          results: {
            abc: '0x1',
          },
        }),
      );
      expect(store.getState()).toEqual({
        callResults: {
          [1]: {
            abc: {
              blockNumber: 2,
              data: '0x2',
            },
          },
        },
      });
    });
  });
  describe('fetchingMulticallResults', () => {
    it('updates state to fetching', () => {
      store.dispatch(
        fetchingMulticallResults({
          chainId: 1,
          fetchingBlockNumber: 2,
          calls: [{ address: DAI_ADDRESS, callData: '0x0' }],
        }),
      );
      expect(store.getState()).toEqual({
        callResults: {
          [1]: {
            [`${DAI_ADDRESS}-0x0`]: { fetchingBlockNumber: 2 },
          },
        },
      });
    });

    it('updates state to fetching even if already fetching older block', () => {
      store.dispatch(
        fetchingMulticallResults({
          chainId: 1,
          fetchingBlockNumber: 2,
          calls: [{ address: DAI_ADDRESS, callData: '0x0' }],
        }),
      );
      store.dispatch(
        fetchingMulticallResults({
          chainId: 1,
          fetchingBlockNumber: 3,
          calls: [{ address: DAI_ADDRESS, callData: '0x0' }],
        }),
      );
      expect(store.getState()).toEqual({
        callResults: {
          [1]: {
            [`${DAI_ADDRESS}-0x0`]: { fetchingBlockNumber: 3 },
          },
        },
      });
    });

    it('does not do update if fetching newer block', () => {
      store.dispatch(
        fetchingMulticallResults({
          chainId: 1,
          fetchingBlockNumber: 2,
          calls: [{ address: DAI_ADDRESS, callData: '0x0' }],
        }),
      );
      store.dispatch(
        fetchingMulticallResults({
          chainId: 1,
          fetchingBlockNumber: 1,
          calls: [{ address: DAI_ADDRESS, callData: '0x0' }],
        }),
      );
      expect(store.getState()).toEqual({
        callResults: {
          [1]: {
            [`${DAI_ADDRESS}-0x0`]: { fetchingBlockNumber: 2 },
          },
        },
      });
    });
  });

  describe('errorFetchingMulticallResults', () => {
    it('does nothing if not fetching', () => {
      store.dispatch(
        errorFetchingMulticallResults({
          chainId: 1,
          fetchingBlockNumber: 1,
          calls: [{ address: DAI_ADDRESS, callData: '0x0' }],
        }),
      );
      expect(store.getState()).toEqual({
        callResults: {
          [1]: {},
        },
      });
    });
    it('updates block number if we were fetching', () => {
      store.dispatch(
        fetchingMulticallResults({
          chainId: 1,
          fetchingBlockNumber: 2,
          calls: [{ address: DAI_ADDRESS, callData: '0x0' }],
        }),
      );
      store.dispatch(
        errorFetchingMulticallResults({
          chainId: 1,
          fetchingBlockNumber: 2,
          calls: [{ address: DAI_ADDRESS, callData: '0x0' }],
        }),
      );
      expect(store.getState()).toEqual({
        callResults: {
          [1]: {
            [`${DAI_ADDRESS}-0x0`]: {
              blockNumber: 2,
              // null data indicates error
              data: null,
            },
          },
        },
      });
    });
    it('does nothing if not errored on latest block', () => {
      store.dispatch(
        fetchingMulticallResults({
          chainId: 1,
          fetchingBlockNumber: 3,
          calls: [{ address: DAI_ADDRESS, callData: '0x0' }],
        }),
      );
      store.dispatch(
        errorFetchingMulticallResults({
          chainId: 1,
          fetchingBlockNumber: 2,
          calls: [{ address: DAI_ADDRESS, callData: '0x0' }],
        }),
      );
      expect(store.getState()).toEqual({
        callResults: {
          [1]: {
            [`${DAI_ADDRESS}-0x0`]: { fetchingBlockNumber: 3 },
          },
        },
      });
    });
  });
});
Example #9
Source File: reducer.test.ts    From sybil-interface with GNU General Public License v3.0 4 votes vote down vote up
describe('multicall reducer', () => {
  let store: Store<MulticallState>
  beforeEach(() => {
    store = createStore(reducer)
  })

  it('has correct initial state', () => {
    expect(store.getState().callResults).toEqual({})
    expect(store.getState().callListeners).toEqual(undefined)
  })

  describe('addMulticallListeners', () => {
    it('adds listeners', () => {
      store.dispatch(
        addMulticallListeners({
          chainId: 1,
          calls: [
            {
              address: DAI_ADDRESS,
              callData: '0x',
            },
          ],
        })
      )
      expect(store.getState()).toEqual({
        callListeners: {
          [1]: {
            [`${DAI_ADDRESS}-0x`]: {
              [1]: 1,
            },
          },
        },
        callResults: {},
      })
    })
  })

  describe('removeMulticallListeners', () => {
    it('noop', () => {
      store.dispatch(
        removeMulticallListeners({
          calls: [
            {
              address: DAI_ADDRESS,
              callData: '0x',
            },
          ],
          chainId: 1,
        })
      )
      expect(store.getState()).toEqual({ callResults: {}, callListeners: {} })
    })
    it('removes listeners', () => {
      store.dispatch(
        addMulticallListeners({
          chainId: 1,
          calls: [
            {
              address: DAI_ADDRESS,
              callData: '0x',
            },
          ],
        })
      )
      store.dispatch(
        removeMulticallListeners({
          calls: [
            {
              address: DAI_ADDRESS,
              callData: '0x',
            },
          ],
          chainId: 1,
        })
      )
      expect(store.getState()).toEqual({
        callResults: {},
        callListeners: { [1]: { [`${DAI_ADDRESS}-0x`]: {} } },
      })
    })
  })

  describe('updateMulticallResults', () => {
    it('updates data if not present', () => {
      store.dispatch(
        updateMulticallResults({
          chainId: 1,
          blockNumber: 1,
          results: {
            abc: '0x',
          },
        })
      )
      expect(store.getState()).toEqual({
        callResults: {
          [1]: {
            abc: {
              blockNumber: 1,
              data: '0x',
            },
          },
        },
      })
    })
    it('updates old data', () => {
      store.dispatch(
        updateMulticallResults({
          chainId: 1,
          blockNumber: 1,
          results: {
            abc: '0x',
          },
        })
      )
      store.dispatch(
        updateMulticallResults({
          chainId: 1,
          blockNumber: 2,
          results: {
            abc: '0x2',
          },
        })
      )
      expect(store.getState()).toEqual({
        callResults: {
          [1]: {
            abc: {
              blockNumber: 2,
              data: '0x2',
            },
          },
        },
      })
    })
    it('ignores late updates', () => {
      store.dispatch(
        updateMulticallResults({
          chainId: 1,
          blockNumber: 2,
          results: {
            abc: '0x2',
          },
        })
      )
      store.dispatch(
        updateMulticallResults({
          chainId: 1,
          blockNumber: 1,
          results: {
            abc: '0x1',
          },
        })
      )
      expect(store.getState()).toEqual({
        callResults: {
          [1]: {
            abc: {
              blockNumber: 2,
              data: '0x2',
            },
          },
        },
      })
    })
  })
  describe('fetchingMulticallResults', () => {
    it('updates state to fetching', () => {
      store.dispatch(
        fetchingMulticallResults({
          chainId: 1,
          fetchingBlockNumber: 2,
          calls: [{ address: DAI_ADDRESS, callData: '0x0' }],
        })
      )
      expect(store.getState()).toEqual({
        callResults: {
          [1]: {
            [`${DAI_ADDRESS}-0x0`]: { fetchingBlockNumber: 2 },
          },
        },
      })
    })

    it('updates state to fetching even if already fetching older block', () => {
      store.dispatch(
        fetchingMulticallResults({
          chainId: 1,
          fetchingBlockNumber: 2,
          calls: [{ address: DAI_ADDRESS, callData: '0x0' }],
        })
      )
      store.dispatch(
        fetchingMulticallResults({
          chainId: 1,
          fetchingBlockNumber: 3,
          calls: [{ address: DAI_ADDRESS, callData: '0x0' }],
        })
      )
      expect(store.getState()).toEqual({
        callResults: {
          [1]: {
            [`${DAI_ADDRESS}-0x0`]: { fetchingBlockNumber: 3 },
          },
        },
      })
    })

    it('does not do update if fetching newer block', () => {
      store.dispatch(
        fetchingMulticallResults({
          chainId: 1,
          fetchingBlockNumber: 2,
          calls: [{ address: DAI_ADDRESS, callData: '0x0' }],
        })
      )
      store.dispatch(
        fetchingMulticallResults({
          chainId: 1,
          fetchingBlockNumber: 1,
          calls: [{ address: DAI_ADDRESS, callData: '0x0' }],
        })
      )
      expect(store.getState()).toEqual({
        callResults: {
          [1]: {
            [`${DAI_ADDRESS}-0x0`]: { fetchingBlockNumber: 2 },
          },
        },
      })
    })
  })

  describe('errorFetchingMulticallResults', () => {
    it('does nothing if not fetching', () => {
      store.dispatch(
        errorFetchingMulticallResults({
          chainId: 1,
          fetchingBlockNumber: 1,
          calls: [{ address: DAI_ADDRESS, callData: '0x0' }],
        })
      )
      expect(store.getState()).toEqual({
        callResults: {
          [1]: {},
        },
      })
    })
    it('updates block number if we were fetching', () => {
      store.dispatch(
        fetchingMulticallResults({
          chainId: 1,
          fetchingBlockNumber: 2,
          calls: [{ address: DAI_ADDRESS, callData: '0x0' }],
        })
      )
      store.dispatch(
        errorFetchingMulticallResults({
          chainId: 1,
          fetchingBlockNumber: 2,
          calls: [{ address: DAI_ADDRESS, callData: '0x0' }],
        })
      )
      expect(store.getState()).toEqual({
        callResults: {
          [1]: {
            [`${DAI_ADDRESS}-0x0`]: {
              blockNumber: 2,
              // null data indicates error
              data: null,
            },
          },
        },
      })
    })
    it('does nothing if not errored on latest block', () => {
      store.dispatch(
        fetchingMulticallResults({
          chainId: 1,
          fetchingBlockNumber: 3,
          calls: [{ address: DAI_ADDRESS, callData: '0x0' }],
        })
      )
      store.dispatch(
        errorFetchingMulticallResults({
          chainId: 1,
          fetchingBlockNumber: 2,
          calls: [{ address: DAI_ADDRESS, callData: '0x0' }],
        })
      )
      expect(store.getState()).toEqual({
        callResults: {
          [1]: {
            [`${DAI_ADDRESS}-0x0`]: { fetchingBlockNumber: 3 },
          },
        },
      })
    })
  })
})
Example #10
Source File: reducer.test.ts    From glide-frontend with GNU General Public License v3.0 4 votes vote down vote up
describe('multicall reducer', () => {
  let store: Store<MulticallState>
  beforeEach(() => {
    store = createStore(reducer)
  })

  it('has correct initial state', () => {
    expect(store.getState().callResults).toEqual({})
    expect(store.getState().callListeners).toEqual(undefined)
  })

  describe('addMulticallListeners', () => {
    it('adds listeners', () => {
      store.dispatch(
        addMulticallListeners({
          chainId: 1,
          calls: [
            {
              address: DAI_ADDRESS,
              callData: '0x',
            },
          ],
        }),
      )
      expect(store.getState()).toEqual({
        callListeners: {
          1: {
            [`${DAI_ADDRESS}-0x`]: {
              1: 1,
            },
          },
        },
        callResults: {},
      })
    })
  })

  describe('removeMulticallListeners', () => {
    it('noop', () => {
      store.dispatch(
        removeMulticallListeners({
          calls: [
            {
              address: DAI_ADDRESS,
              callData: '0x',
            },
          ],
          chainId: 1,
        }),
      )
      expect(store.getState()).toEqual({ callResults: {}, callListeners: {} })
    })
    it('removes listeners', () => {
      store.dispatch(
        addMulticallListeners({
          chainId: 1,
          calls: [
            {
              address: DAI_ADDRESS,
              callData: '0x',
            },
          ],
        }),
      )
      store.dispatch(
        removeMulticallListeners({
          calls: [
            {
              address: DAI_ADDRESS,
              callData: '0x',
            },
          ],
          chainId: 1,
        }),
      )
      expect(store.getState()).toEqual({
        callResults: {},
        callListeners: { 1: { [`${DAI_ADDRESS}-0x`]: {} } },
      })
    })
  })

  describe('updateMulticallResults', () => {
    it('updates data if not present', () => {
      store.dispatch(
        updateMulticallResults({
          chainId: 1,
          blockNumber: 1,
          results: {
            abc: '0x',
          },
        }),
      )
      expect(store.getState()).toEqual({
        callResults: {
          1: {
            abc: {
              blockNumber: 1,
              data: '0x',
            },
          },
        },
      })
    })
    it('updates old data', () => {
      store.dispatch(
        updateMulticallResults({
          chainId: 1,
          blockNumber: 1,
          results: {
            abc: '0x',
          },
        }),
      )
      store.dispatch(
        updateMulticallResults({
          chainId: 1,
          blockNumber: 2,
          results: {
            abc: '0x2',
          },
        }),
      )
      expect(store.getState()).toEqual({
        callResults: {
          1: {
            abc: {
              blockNumber: 2,
              data: '0x2',
            },
          },
        },
      })
    })
    it('ignores late updates', () => {
      store.dispatch(
        updateMulticallResults({
          chainId: 1,
          blockNumber: 2,
          results: {
            abc: '0x2',
          },
        }),
      )
      store.dispatch(
        updateMulticallResults({
          chainId: 1,
          blockNumber: 1,
          results: {
            abc: '0x1',
          },
        }),
      )
      expect(store.getState()).toEqual({
        callResults: {
          1: {
            abc: {
              blockNumber: 2,
              data: '0x2',
            },
          },
        },
      })
    })
  })
  describe('fetchingMulticallResults', () => {
    it('updates state to fetching', () => {
      store.dispatch(
        fetchingMulticallResults({
          chainId: 1,
          fetchingBlockNumber: 2,
          calls: [{ address: DAI_ADDRESS, callData: '0x0' }],
        }),
      )
      expect(store.getState()).toEqual({
        callResults: {
          1: {
            [`${DAI_ADDRESS}-0x0`]: { fetchingBlockNumber: 2 },
          },
        },
      })
    })

    it('updates state to fetching even if already fetching older block', () => {
      store.dispatch(
        fetchingMulticallResults({
          chainId: 1,
          fetchingBlockNumber: 2,
          calls: [{ address: DAI_ADDRESS, callData: '0x0' }],
        }),
      )
      store.dispatch(
        fetchingMulticallResults({
          chainId: 1,
          fetchingBlockNumber: 3,
          calls: [{ address: DAI_ADDRESS, callData: '0x0' }],
        }),
      )
      expect(store.getState()).toEqual({
        callResults: {
          1: {
            [`${DAI_ADDRESS}-0x0`]: { fetchingBlockNumber: 3 },
          },
        },
      })
    })

    it('does not do update if fetching newer block', () => {
      store.dispatch(
        fetchingMulticallResults({
          chainId: 1,
          fetchingBlockNumber: 2,
          calls: [{ address: DAI_ADDRESS, callData: '0x0' }],
        }),
      )
      store.dispatch(
        fetchingMulticallResults({
          chainId: 1,
          fetchingBlockNumber: 1,
          calls: [{ address: DAI_ADDRESS, callData: '0x0' }],
        }),
      )
      expect(store.getState()).toEqual({
        callResults: {
          1: {
            [`${DAI_ADDRESS}-0x0`]: { fetchingBlockNumber: 2 },
          },
        },
      })
    })
  })

  describe('errorFetchingMulticallResults', () => {
    it('does nothing if not fetching', () => {
      store.dispatch(
        errorFetchingMulticallResults({
          chainId: 1,
          fetchingBlockNumber: 1,
          calls: [{ address: DAI_ADDRESS, callData: '0x0' }],
        }),
      )
      expect(store.getState()).toEqual({
        callResults: {
          1: {},
        },
      })
    })
    it('updates block number if we were fetching', () => {
      store.dispatch(
        fetchingMulticallResults({
          chainId: 1,
          fetchingBlockNumber: 2,
          calls: [{ address: DAI_ADDRESS, callData: '0x0' }],
        }),
      )
      store.dispatch(
        errorFetchingMulticallResults({
          chainId: 1,
          fetchingBlockNumber: 2,
          calls: [{ address: DAI_ADDRESS, callData: '0x0' }],
        }),
      )
      expect(store.getState()).toEqual({
        callResults: {
          1: {
            [`${DAI_ADDRESS}-0x0`]: {
              blockNumber: 2,
              // null data indicates error
              data: null,
            },
          },
        },
      })
    })
    it('does nothing if not errored on latest block', () => {
      store.dispatch(
        fetchingMulticallResults({
          chainId: 1,
          fetchingBlockNumber: 3,
          calls: [{ address: DAI_ADDRESS, callData: '0x0' }],
        }),
      )
      store.dispatch(
        errorFetchingMulticallResults({
          chainId: 1,
          fetchingBlockNumber: 2,
          calls: [{ address: DAI_ADDRESS, callData: '0x0' }],
        }),
      )
      expect(store.getState()).toEqual({
        callResults: {
          1: {
            [`${DAI_ADDRESS}-0x0`]: { fetchingBlockNumber: 3 },
          },
        },
      })
    })
  })
})
Example #11
Source File: reducer.test.ts    From goose-frontend-amm with GNU General Public License v3.0 4 votes vote down vote up
describe('multicall reducer', () => {
  let store: Store<MulticallState>
  beforeEach(() => {
    store = createStore(reducer)
  })

  it('has correct initial state', () => {
    expect(store.getState().callResults).toEqual({})
    expect(store.getState().callListeners).toEqual(undefined)
  })

  describe('addMulticallListeners', () => {
    it('adds listeners', () => {
      store.dispatch(
        addMulticallListeners({
          chainId: 1,
          calls: [
            {
              address: DAI_ADDRESS,
              callData: '0x'
            }
          ]
        })
      )
      expect(store.getState()).toEqual({
        callListeners: {
          1: {
            [`${DAI_ADDRESS}-0x`]: {
              1: 1
            }
          }
        },
        callResults: {}
      })
    })
  })

  describe('removeMulticallListeners', () => {
    it('noop', () => {
      store.dispatch(
        removeMulticallListeners({
          calls: [
            {
              address: DAI_ADDRESS,
              callData: '0x'
            }
          ],
          chainId: 1
        })
      )
      expect(store.getState()).toEqual({ callResults: {}, callListeners: {} })
    })
    it('removes listeners', () => {
      store.dispatch(
        addMulticallListeners({
          chainId: 1,
          calls: [
            {
              address: DAI_ADDRESS,
              callData: '0x'
            }
          ]
        })
      )
      store.dispatch(
        removeMulticallListeners({
          calls: [
            {
              address: DAI_ADDRESS,
              callData: '0x'
            }
          ],
          chainId: 1
        })
      )
      expect(store.getState()).toEqual({
        callResults: {},
        callListeners: { 1: { [`${DAI_ADDRESS}-0x`]: {} } }
      })
    })
  })

  describe('updateMulticallResults', () => {
    it('updates data if not present', () => {
      store.dispatch(
        updateMulticallResults({
          chainId: 1,
          blockNumber: 1,
          results: {
            abc: '0x'
          }
        })
      )
      expect(store.getState()).toEqual({
        callResults: {
          1: {
            abc: {
              blockNumber: 1,
              data: '0x'
            }
          }
        }
      })
    })
    it('updates old data', () => {
      store.dispatch(
        updateMulticallResults({
          chainId: 1,
          blockNumber: 1,
          results: {
            abc: '0x'
          }
        })
      )
      store.dispatch(
        updateMulticallResults({
          chainId: 1,
          blockNumber: 2,
          results: {
            abc: '0x2'
          }
        })
      )
      expect(store.getState()).toEqual({
        callResults: {
          1: {
            abc: {
              blockNumber: 2,
              data: '0x2'
            }
          }
        }
      })
    })
    it('ignores late updates', () => {
      store.dispatch(
        updateMulticallResults({
          chainId: 1,
          blockNumber: 2,
          results: {
            abc: '0x2'
          }
        })
      )
      store.dispatch(
        updateMulticallResults({
          chainId: 1,
          blockNumber: 1,
          results: {
            abc: '0x1'
          }
        })
      )
      expect(store.getState()).toEqual({
        callResults: {
          1: {
            abc: {
              blockNumber: 2,
              data: '0x2'
            }
          }
        }
      })
    })
  })
  describe('fetchingMulticallResults', () => {
    it('updates state to fetching', () => {
      store.dispatch(
        fetchingMulticallResults({
          chainId: 1,
          fetchingBlockNumber: 2,
          calls: [{ address: DAI_ADDRESS, callData: '0x0' }]
        })
      )
      expect(store.getState()).toEqual({
        callResults: {
          1: {
            [`${DAI_ADDRESS}-0x0`]: { fetchingBlockNumber: 2 }
          }
        }
      })
    })

    it('updates state to fetching even if already fetching older block', () => {
      store.dispatch(
        fetchingMulticallResults({
          chainId: 1,
          fetchingBlockNumber: 2,
          calls: [{ address: DAI_ADDRESS, callData: '0x0' }]
        })
      )
      store.dispatch(
        fetchingMulticallResults({
          chainId: 1,
          fetchingBlockNumber: 3,
          calls: [{ address: DAI_ADDRESS, callData: '0x0' }]
        })
      )
      expect(store.getState()).toEqual({
        callResults: {
          1: {
            [`${DAI_ADDRESS}-0x0`]: { fetchingBlockNumber: 3 }
          }
        }
      })
    })

    it('does not do update if fetching newer block', () => {
      store.dispatch(
        fetchingMulticallResults({
          chainId: 1,
          fetchingBlockNumber: 2,
          calls: [{ address: DAI_ADDRESS, callData: '0x0' }]
        })
      )
      store.dispatch(
        fetchingMulticallResults({
          chainId: 1,
          fetchingBlockNumber: 1,
          calls: [{ address: DAI_ADDRESS, callData: '0x0' }]
        })
      )
      expect(store.getState()).toEqual({
        callResults: {
          1: {
            [`${DAI_ADDRESS}-0x0`]: { fetchingBlockNumber: 2 }
          }
        }
      })
    })
  })

  describe('errorFetchingMulticallResults', () => {
    it('does nothing if not fetching', () => {
      store.dispatch(
        errorFetchingMulticallResults({
          chainId: 1,
          fetchingBlockNumber: 1,
          calls: [{ address: DAI_ADDRESS, callData: '0x0' }]
        })
      )
      expect(store.getState()).toEqual({
        callResults: {
          1: {}
        }
      })
    })
    it('updates block number if we were fetching', () => {
      store.dispatch(
        fetchingMulticallResults({
          chainId: 1,
          fetchingBlockNumber: 2,
          calls: [{ address: DAI_ADDRESS, callData: '0x0' }]
        })
      )
      store.dispatch(
        errorFetchingMulticallResults({
          chainId: 1,
          fetchingBlockNumber: 2,
          calls: [{ address: DAI_ADDRESS, callData: '0x0' }]
        })
      )
      expect(store.getState()).toEqual({
        callResults: {
          1: {
            [`${DAI_ADDRESS}-0x0`]: {
              blockNumber: 2,
              // null data indicates error
              data: null
            }
          }
        }
      })
    })
    it('does nothing if not errored on latest block', () => {
      store.dispatch(
        fetchingMulticallResults({
          chainId: 1,
          fetchingBlockNumber: 3,
          calls: [{ address: DAI_ADDRESS, callData: '0x0' }]
        })
      )
      store.dispatch(
        errorFetchingMulticallResults({
          chainId: 1,
          fetchingBlockNumber: 2,
          calls: [{ address: DAI_ADDRESS, callData: '0x0' }]
        })
      )
      expect(store.getState()).toEqual({
        callResults: {
          1: {
            [`${DAI_ADDRESS}-0x0`]: { fetchingBlockNumber: 3 }
          }
        }
      })
    })
  })
})