Skip to content

Commit

Permalink
Merge pull request #103 from guardian/fix-toggle-auto-update
Browse files Browse the repository at this point in the history
Newswires UI: fix toggle auto-update
  • Loading branch information
sb-dev authored Jan 21, 2025
2 parents d9042b2 + 8e2d05c commit 924c62a
Show file tree
Hide file tree
Showing 2 changed files with 127 additions and 214 deletions.
245 changes: 87 additions & 158 deletions newswires/client/src/context/SearchReducer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,28 @@ describe('SearchReducer', () => {
autoUpdate: true,
};

const successState: State = {
...initialState,
status: 'success',
queryData: { results: [sampleWireData] },
};

const offlineState: State = {
status: 'offline',
queryData: { results: [sampleWireData] },
successfulQueryHistory: [],
error: 'Network error',
autoUpdate: true,
};

const errorState: State = {
status: 'error',
queryData: { results: [sampleWireData] },
successfulQueryHistory: [],
error: 'Fetch error',
autoUpdate: true,
};

it('should handle FETCH_SUCCESS action in loading state', () => {
const action: Action = {
type: 'FETCH_SUCCESS',
Expand All @@ -26,185 +48,92 @@ describe('SearchReducer', () => {
expect(newState.error).toBeUndefined();
});

it('should handle FETCH_ERROR action in loading state', () => {
const action: Action = {
type: 'FETCH_ERROR',
error: 'Fetch failed',
};

const newState = SearchReducer(initialState, action);

expect(newState.status).toBe('error');
expect(newState.error).toEqual(action.error);
});

it('should handle UPDATE_RESULTS action in success state', () => {
const state: State = {
...initialState,
status: 'success',
queryData: { results: [{ ...sampleWireData, id: 1 }] },
};

const action: Action = {
type: 'UPDATE_RESULTS',
data: { results: [{ ...sampleWireData, id: 2 }] },
};

const newState = SearchReducer(state, action);

expect(newState.status).toBe('success');
expect(newState.queryData?.results).toHaveLength(2);
expect(newState.queryData?.results).toContainEqual({
...sampleWireData,
id: 2,
isFromRefresh: true,
});
expect(newState.queryData?.results).toContainEqual({
...sampleWireData,
id: 1,
[
{
state: {
...initialState,
autoUpdate: true,
},
expectedAutoUpdateValue: false,
},
{
state: {
...successState,
autoUpdate: true,
},
expectedAutoUpdateValue: false,
},
].forEach(({ state, expectedAutoUpdateValue }) => {
it(`should handle TOGGLE_AUTO_UPDATE action in ${state.status} state`, () => {
const action: Action = {
type: 'TOGGLE_AUTO_UPDATE',
};

const newState = SearchReducer(state, action);

expect(newState.autoUpdate).toBe(expectedAutoUpdateValue);
});
});

it('should handle ENTER_QUERY action in success state', () => {
const state: State = {
...initialState,
status: 'success',
queryData: { results: [sampleWireData] },
};

const action: Action = {
type: 'ENTER_QUERY',
};

const newState = SearchReducer(state, action);

expect(newState.status).toBe('loading');
});

it('should handle TOGGLE_AUTO_UPDATE action in success state', () => {
const state: State = {
...initialState,
status: 'success',
queryData: { results: [sampleWireData] },
};

const action: Action = {
type: 'TOGGLE_AUTO_UPDATE',
};

const newState = SearchReducer(state, action);

expect(newState.autoUpdate).toBe(false);
});

it('should handle FETCH_ERROR action in success state', () => {
const state: State = {
...initialState,
status: 'success',
queryData: { results: [sampleWireData] },
};

const action: Action = {
type: 'FETCH_ERROR',
error: 'Fetch failed',
};

const newState = SearchReducer(state, action);

expect(newState.status).toBe('offline');
expect(newState.error).toEqual(action.error);
});

it('should handle UPDATE_RESULTS action in offline state', () => {
const state: State = {
status: 'offline',
queryData: { results: [{ ...sampleWireData, id: 1 }] },
successfulQueryHistory: [],
error: 'Network error',
autoUpdate: true,
};

const action: Action = {
type: 'UPDATE_RESULTS',
data: { results: [{ ...sampleWireData, id: 2 }] },
};

const newState = SearchReducer(state, action);

expect(newState.status).toBe('success');
expect(newState.queryData?.results).toHaveLength(2);
expect(newState.queryData?.results).toContainEqual({
...sampleWireData,
id: 2,
isFromRefresh: true,
});
expect(newState.queryData?.results).toContainEqual({
...sampleWireData,
id: 1,
[successState, offlineState, errorState].forEach((state) => {
it(`should handle UPDATE_RESULTS action in ${state.status} state`, () => {
const action: Action = {
type: 'UPDATE_RESULTS',
data: { results: [{ ...sampleWireData, id: 2 }] },
};

const newState = SearchReducer(state, action);

expect(newState.status).toBe('success');
expect(newState.queryData?.results).toHaveLength(2);
expect(newState.queryData?.results).toContainEqual({
...sampleWireData,
id: 2,
isFromRefresh: true,
});
expect(newState.queryData?.results).toContainEqual({
...sampleWireData,
id: 1,
});
});
});

it('should handle UPDATE_RESULTS action in error state', () => {
const state: State = {
status: 'error',
queryData: { results: [{ ...sampleWireData, id: 1 }] },
successfulQueryHistory: [],
error: 'Fetch error',
autoUpdate: true,
};
[
{ state: initialState, expectedStatus: 'error' },
{ state: successState, expectedStatus: 'offline' },
].forEach(({ state, expectedStatus }) => {
it(`should handle FETCH_ERROR action in ${state.status} state`, () => {
const action: Action = {
type: 'FETCH_ERROR',
error: 'Fetch failed',
};

const action: Action = {
type: 'UPDATE_RESULTS',
data: { results: [{ ...sampleWireData, id: 2 }] },
};
const newState = SearchReducer(state, action);

const newState = SearchReducer(state, action);

expect(newState.status).toBe('success');
expect(newState.queryData?.results).toHaveLength(2);
expect(newState.queryData?.results).toContainEqual({
...sampleWireData,
id: 2,
isFromRefresh: true,
});
expect(newState.queryData?.results).toContainEqual({
...sampleWireData,
id: 1,
expect(newState.status).toBe(expectedStatus);
expect(newState.error).toEqual(action.error);
});
});

it('should handle RETRY action in error state', () => {
const state: State = {
status: 'error',
successfulQueryHistory: [],
error: 'Fetch error',
autoUpdate: true,
};

const action: Action = {
type: 'RETRY',
};

const newState = SearchReducer(state, action);
const newState = SearchReducer(errorState, action);

expect(newState.status).toBe('loading');
});

it('should handle ENTER_QUERY action in error state', () => {
const state: State = {
status: 'error',
queryData: undefined,
successfulQueryHistory: [],
error: 'Fetch error',
autoUpdate: true,
};
[successState, offlineState, errorState].forEach((state) => {
it(`should handle ENTER_QUERY action in ${state.status} state`, () => {
const action: Action = {
type: 'ENTER_QUERY',
};

const action: Action = {
type: 'ENTER_QUERY',
};

const newState = SearchReducer(state, action);
const newState = SearchReducer(state, action);

expect(newState.status).toBe('loading');
expect(newState.status).toBe('loading');
});
});
});
Loading

0 comments on commit 924c62a

Please sign in to comment.