{"size": 2379, "ext": "js", "max_stars_repo_path": "src/store/interceptor/reducer.test.js", "max_stars_repo_name": "pshrabstein/mymove", "max_stars_repo_stars_event_min_datetime": "2018-02-08T19:15:16.000Z", "max_stars_repo_stars_event_max_datetime": "2022-03-18T17:52:04.000Z", "max_issues_repo_path": "src/store/interceptor/reducer.test.js", "max_issues_repo_name": "transcom/mymove", "max_issues_repo_issues_event_min_datetime": "2018-01-19T01:11:09.000Z", "max_issues_repo_issues_event_max_datetime": "2022-03-31T21:46:17.000Z", "max_forks_repo_path": "src/store/interceptor/reducer.test.js", "max_forks_repo_name": "pshrabstein/mymove", "max_forks_repo_forks_event_min_datetime": "2018-02-01T22:31:08.000Z", "max_forks_repo_forks_event_max_datetime": "2022-03-25T17:43:10.000Z"}
{"max_stars_count": 46, "max_issues_count": 5729, "max_forks_count": 39, "avg_line_length": 38.3709677419, "max_line_length": 102, "alphanum_fraction": 0.6868432114}
import { interceptResponse } from './actions';
import interceptorReducer, { initialState } from './reducer';

const SHORT_RESPONSE_GAP_MS = 100;
const LONG_RESPONSE_GAP_MS = 4000;

describe('interceptorReducer', () => {
  it('returns the initial state by default', () => {
    expect(interceptorReducer(undefined, undefined)).toEqual(initialState);
  });

  describe('INTERCEPT_RESPONSE', () => {
    it('updates state if an error was intercepted', () => {
      const oldState = {
        hasRecentError: false,
        timestamp: Date.now() - SHORT_RESPONSE_GAP_MS,
        traceId: '',
      };
      const traceId = 'some-trace-id';
      const newState = interceptorReducer(oldState, interceptResponse(true, traceId));
      expect(newState.hasRecentError).toEqual(true);
      expect(newState.timestamp - oldState.timestamp).toBeGreaterThanOrEqual(SHORT_RESPONSE_GAP_MS);
      expect(newState.traceId).toBe(traceId);
    });

    it('maintains hasRecentError value if a recent response was intercepted', () => {
      const oldState = {
        hasRecentError: true,
        timestamp: Date.now() - SHORT_RESPONSE_GAP_MS,
      };

      const newState = interceptorReducer(oldState, interceptResponse(false));
      expect(newState.hasRecentError).toEqual(true);
      expect(newState.timestamp - oldState.timestamp).toBeGreaterThanOrEqual(SHORT_RESPONSE_GAP_MS);
    });

    it('updates hasRecentError value if timestamp is stale and clears traceId', () => {
      const oldState = {
        hasRecentError: true,
        timestamp: Date.now() - LONG_RESPONSE_GAP_MS,
        traceId: 'some-trace-id',
      };

      const newState = interceptorReducer(oldState, interceptResponse(false));
      expect(newState.hasRecentError).toEqual(false);
      expect(newState.timestamp - oldState.timestamp).toBeGreaterThanOrEqual(LONG_RESPONSE_GAP_MS);
      expect(newState.traceId).toBe('');
    });

    it('maintains hasRecentError value if action value is false and state timestamp is stale', () => {
      const oldState = {
        hasRecentError: false,
        timestamp: Date.now() - LONG_RESPONSE_GAP_MS,
      };

      const newState = interceptorReducer(oldState, interceptResponse(false));
      expect(newState.hasRecentError).toEqual(false);
      expect(newState.timestamp - oldState.timestamp).toBeGreaterThanOrEqual(LONG_RESPONSE_GAP_MS);
    });
  });
});
