npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

spy-middleware

v1.2.2

Published

a redux middleware for spying actions

Downloads

403

Readme

spyMiddleware building status

Middleware that spyies all actions and allows you to wait for specific actions or events.

Quick Use

Install with npm:

npm install spy-middleware

or for just testing:

npm install -D spy-middleware

Use in your code:

import { createStore, applyMiddleware } from 'redux'
import makeSpyMiddleware from 'spy-middleware'

import { fetchTopic, PUT_TOPIC } from '../actions'
import middleware from '../middleware'
import reducer from '../reducer'
import { getTopic } from '../selectors'

let spyMiddleware
let store
beforeEach(() => {
  spyMiddleware = makeSpyMiddleware()
  store = createStore(reducer, applyMiddleware(middleware, spyMiddleware))
})

test("when a fetchTopic is requested it retrieves the topic from the REST service", async () => {
  fetch.mockResponse('This is the readme')
  store.dispatch(fetchTopic('README'))

  await spyMiddleware.untilNext(PUT_TOPIC)

  const topic = getTopic(store.getState(), { name: 'README' })
  expect(topic).toEqual('This is the readme')
})

makeSpyMiddleware()

It creates a middleware compatible with redux applyMiddleware that contains specific methods to spy what is happening inside the store.

import makeSpyMiddleware from 'spy-middleware'

let spyMiddleware
beforeEach(() => {
  spyMiddleware = makeSpyMiddleware()
})

spyMiddleware

Redux middleware that has to be applied with applyMiddleware

let store
beforeEach(() => {
  store = createStore(reducer, applyMiddleware(spyMiddleware))
})

### spyMiddleware.getActions(): Action[]

Get all dispatched and reduced actions.

store.dispatch(startAction)
store.dispatch(stopAction)
store.dispatch(resumeAction)

const actions = spyMiddleware.getActions()
expect(actions).toEqual([startAction, stopAction, resumeAction])

### spyMiddleware.getAction(string|regExp|(action)=>bool): Action

Get the last action that satisfies the condition or undefined if none.

store.dispatch(startAction)
store.dispatch(stopAction)
store.dispatch(resumeAction)

const foundAction = spyMiddleware.getAction(/^S/i)
expect(foundAction).toBe(stopAction)

### spyMiddleware.untilNext(string|regExp|(action)=>bool): Promise

Returns a promise which is resolved when the matching action is dispatched and reduced.

let actionsBeforeAwait
let actionsAfterAwait

delay(1).then(() => store.dispatch(startAction))
actionsBeforeAwait = spyMiddleware.getActions()
await spyMiddleware.untilNext(START)
actionsAfterAwait = spyMiddleware.getActions()

expect(actionsBeforeAwait).toEqual([])
expect(actionsAfterAwait).toEqual([startAction])

The promise is resolved with the first action fired after untilNext call that satisfies the condition.

delay(1).then(() => store.dispatch(startAction))
const foundStartAction = await spyMiddleware.untilNext(START)

expect(foundStartAction).toBe(startAction)

### spyMiddleware.until(string|regExp|(action)=>bool): Promise

Returns a promise which is resolved when the matching action is dispatched and reduced or if a matching action was resolved in the past.

let actionFoundByUntil
let actionFoundByUntilNext

store.dispatch(startAction)
const untilFinished = spyMiddleware.until(action => {
  if (/^S/.test(action.type)) {
    actionFoundByUntil = action
    return true
  }
  return false
})
const untilNextFinished = spyMiddleware.untilNext(action => {
  if (/^S/.test(action.type)) {
    actionFoundByUntilNext = action
    return true
  }
  return false
})
store.dispatch(stopAction)

await untilFinished
await untilNextFinished

expect(actionFoundByUntil).toBe(startAction)
expect(actionFoundByUntilNext).toBe(stopAction)

The promise is resolved with the first action ever dispatched that satisfies the condition.

store.dispatch(startAction)
const foundStartAction = await spyMiddleware.untilNext(START)

expect(foundStartAction).toBe(startAction)

### ### spyMiddleware.clearActions(): void

Clears the list of dispatched actions.

store.dispatch(startAction)
store.dispatch(stopAction)

spyMiddleware.clearActions()

store.dispatch(resumeAction)

const actions = spyMiddleware.getActions()

expect(actions).toEqual([resumeAction])