grand_central icon indicating copy to clipboard operation
grand_central copied to clipboard

Allow actions to be forked for testing

Open jgaskins opened this issue 6 years ago • 0 comments

This lets you fork an action in testing so you don't overwrite its actual store for integration tests. Here is a simple example:

Action = GrandCentral::Action.create
SetName = Action.with_attributes(:name)
AppState = GrandCentral::Model.with_attributes(:name)

initial_state = AppState.new(name: nil)
Store = GrandCentral::Store.new(initial_state) do |state, action|
  case action
  when SetName
    state.update name: action.name
  else state
  end
end

RSpec.describe SetName do
  let(:store) { Store.dup }
  let(:klass) { SetName.fork }
  before { klass.store = store }

  it 'sets the name' do
    klass.call 'My Name'

    expect(store.state.name).to eq 'My Name'
  end
end

Since the forked action will still match the same as the original in a case statement, it still gets picked up in the production store's state-reducer block.

jgaskins avatar Mar 20 '18 03:03 jgaskins