const cloverleaf = require('../../index');
const sinon = require('sinon');

describe('stateSnapshot.discardStateSnapshot', () => {
  it('should call the backing store discard method', async () => {
    const mockBackingStore = {
      commit() {},
      discard: sinon.spy(),
      select: () => ({ results: { itemType: [{ test: 'item' }] }, meta: {} }),
    };
    const store = cloverleaf.createStore(mockBackingStore);
    const fakeReducer = (state, action) => action;
    store.registerReducer(cloverleaf.createReducer(fakeReducer));
    const originalSnapshot = await store.getStateSnapshotBySelector({
      test: 'selector',
    });
    const reducedSnapshot = await originalSnapshot.dispatchToStateSnapshot({
      reducer: 'reduced',
    });
    await reducedSnapshot.discardStateSnapshot();

    mockBackingStore.discard.should.be.called();
  });

  it('should return a new state snapshot', async () => {
    const mockBackingStore = {
      commit() {},
      discard: sinon.spy(),
      select: () => ({ results: { itemType: [{ test: 'item' }] }, meta: {} }),
    };
    const store = cloverleaf.createStore(mockBackingStore);
    const fakeReducer = (state, action) => action;
    store.registerReducer(cloverleaf.createReducer(fakeReducer));
    const originalSnapshot = await store.getStateSnapshotBySelector({
      test: 'selector',
    });
    const reducedSnapshot = await originalSnapshot.dispatchToStateSnapshot({
      reducer: 'reduced',
    });
    const revertedSnapshot = await reducedSnapshot.discardStateSnapshot();

    revertedSnapshot.items.should.not.deepEqual(reducedSnapshot.items);
  });

  it('should return a state snapshot matching the original state', async () => {
    const mockBackingStore = {
      commit() {},
      discard: sinon.spy(),
      select: () => ({ results: { itemType: [{ test: 'item' }] }, meta: {} }),
    };
    const store = cloverleaf.createStore(mockBackingStore);
    const fakeReducer = (state, action) => action;
    store.registerReducer(cloverleaf.createReducer(fakeReducer));
    const originalSnapshot = await store.getStateSnapshotBySelector({
      test: 'selector',
    });
    const reducedSnapshot = await originalSnapshot.dispatchToStateSnapshot({
      reducer: 'reduced',
    });
    const revertedSnapshot = await reducedSnapshot.discardStateSnapshot();

    revertedSnapshot.items.should.deepEqual(originalSnapshot.items);
  });
});

@@ -2403,13 +2403,841 @@
 );%0A  %7D);
+%0A%0A  context('backing store returns a promise that rejects', () =%3E %7B%0A    it('should reject with the same error as the backing store', async () =%3E %7B%0A      const mockBackingStore = %7B%0A        commit() %7B%7D,%0A        discard: sinon.stub().rejects(),%0A        select: () =%3E (%7B results: %7B itemType: %5B%7B test: 'item' %7D%5D %7D, meta: %7B%7D %7D),%0A      %7D;%0A      const store = cloverleaf.createStore(mockBackingStore);%0A      const fakeReducer = (state, action) =%3E action;%0A      store.registerReducer(cloverleaf.createReducer(fakeReducer));%0A      const originalSnapshot = await store.getStateSnapshotBySelector(%7B%0A        test: 'selector',%0A      %7D);%0A      const reducedSnapshot = await originalSnapshot.dispatchToStateSnapshot(%7B%0A        reducer: 'reduced',%0A      %7D);%0A%0A      return reducedSnapshot.discardStateSnapshot().should.be.rejected();%0A    %7D);%0A  %7D);
 %0A%7D);%0A

Add forgotten test for situation where backing store rejects a state discard