<commit_before>import { Store } from '../../projects/ng-state/src/lib/store/store';
import { NgStateTestBed } from '../../projects/ng-state/src/lib/ng-state.test-bed';
import { StateHistory } from '../../projects/ng-state/src/lib/state/history';
import { ServiceLocator } from '../../projects/ng-state/src/public-api';
import { OptimistaicUpdatesManager } from '../../projects/ng-state/src/lib/store/plugins/optimistic-updates.plugin';
import { ImmutableJsDataStrategy } from '../../projects/immutablejs-data-strategy/src/public-api';

describe('Optimistic updates - Immutable', () => {
    let store: Store<any>;
    let stateHistory: StateHistory;

    const dataStrategy = new ImmutableJsDataStrategy();

    beforeEach(() => {
        NgStateTestBed.setTestEnvironment(dataStrategy);
        const initialState = {
            layout: { test: 'test' },
            counter: 1
        };
        store = NgStateTestBed.createStore(initialState);
        stateHistory = ServiceLocator.injector.get(StateHistory);
        stateHistory.history = [];
        store.select(['layout']).update(state => state.set('test', 'test2'));
    });

    afterEach(() => {
        jest.resetAllMocks();
    });

    it('should add tag on history', () => {
        store.optimisticUpdates.tagCurrentState('testTag');
        expect(stateHistory.history[0].tag).toEqual('testTag');
    });

    it('should revert to tag on root level', () => {
        store.select(['layout']).update(state => state.set('test', 'test5'));
        store.optimisticUpdates.tagCurrentState('testTag');

        store.select(['layout']).update(state => state.set('test', 'test3'));
        expect(stateHistory.currentState.getIn(['layout', 'test'])).toEqual('test3');

        store.optimisticUpdates.revertToTag('testTag');

        expect(stateHistory.currentState.getIn(['layout', 'test'])).toEqual('test5');
        expect(stateHistory.history.length).toBe(2);
        expect(stateHistory.history[1].state.getIn(['layout', 'test'])).toEqual('test5');
    });

    it('should revert to tag on nested level', () => {
        store.optimisticUpdates.tagCurrentState('testTag');
        store.update(state => {
            state.setIn(['layout', 'test'], 'test3');
            state.set('counter', 2);
        });

        expect(stateHistory.currentState.getIn(['layout', 'test'])).toEqual('test3');<commit_msg>Fixed test<commit_after>import { Store } from '../../projects/ng-state/src/lib/store/store';
import { NgStateTestBed } from '../../projects/ng-state/src/lib/ng-state.test-bed';
import { StateHistory } from '../../projects/ng-state/src/lib/state/history';
import { ServiceLocator } from '../../projects/ng-state/src/public-api';
import { OptimistaicUpdatesManager } from '../../projects/ng-state/src/lib/store/plugins/optimistic-updates.plugin';
import { ImmutableJsDataStrategy, ImmutableJsDataStrategyModule } from '../../projects/immutablejs-data-strategy/src/public-api';

describe('Optimistic updates - Immutable', () => {
    let store: Store<any>;
    let stateHistory: StateHistory;

    const dataStrategy = new ImmutableJsDataStrategy();

    beforeEach(() => {
        NgStateTestBed.setTestEnvironment(dataStrategy);
        const initialState = {
            layout: { test: 'test' },
            counter: 1
        };
        store = NgStateTestBed.createStore(initialState);
        stateHistory = ServiceLocator.injector.get(StateHistory);
        stateHistory.history = [];
        store.select(['layout']).update(state => state.set('test', 'test2'));
    });

    afterEach(() => {
        jest.resetAllMocks();
    });

    it('should add tag on history', () => {
        store.optimisticUpdates.tagCurrentState('testTag');
        expect(stateHistory.history[0].tag).toEqual('testTag');
    });

    it('should revert to tag on root level', () => {
        store.select(['layout']).update(state => state.set('test', 'test5'));
        store.optimisticUpdates.tagCurrentState('testTag');

        store.select(['layout']).update(state => state.set('test', 'test3'));
        expect(stateHistory.currentState.getIn(['layout', 'test'])).toEqual('test3');

        store.optimisticUpdates.revertToTag('testTag');

        expect(stateHistory.currentState.getIn(['layout', 'test'])).toEqual('test5');
        expect(stateHistory.history.length).toBe(2);
        expect(stateHistory.history[1].state.getIn(['layout', 'test'])).toEqual('test5');
    });

    it('should revert to tag on nested level', () => {
        store.optimisticUpdates.tagCurrentState('testTag');
        store.update(state => {
            state.setIn(['layout', 'test'], 'test3');
            state.set('counter', 2);
        }, { withMutations: true } as ImmutableJsDataStrategyModule);

        expect(stateHistory.currentState.getIn(['layout', 'test'])).toEqual('test3');
{'fraction_non_alphanumeric': 0.18093465674110834, 'fraction_numerical': 0.005376344086021506, 'mean_word_length': 3.33034914950761, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2637954', 'n_tokens_mistral': 1398, 'n_tokens_neox': 1387, 'n_words': 207}