Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
127 changes: 126 additions & 1 deletion src/components/simulator/Simulator.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,137 @@ const { setup } = composeComponentTestUtils<SimulatorProps>(Simulator, {
},
liveActivity: null,
mergeEditorState: jest.fn(),
language: null
onToggled: jest.fn(),
popped: ''
});

describe(Simulator.name, () => {
it('renders', () => {
const { wrapper } = setup();
expect(wrapper).toMatchSnapshot();
});

describe('updateRunContext edge cases', () => {
it('handles null events array in runContext', () => {
const { instance } = setup();
const simulator = instance as any;

// Create a partial mock to test just the problematic code path
const runContext = {
contact: { uuid: 'test', urns: [], status: 'active', fields: {}, groups: [], created_on: '2020-01-01T00:00:00Z' },
session: { runs: [], contact: null, status: 'completed' },
context: {},
events: null, // This should cause the unshift error
segments: []
};

const msgEvt = { uuid: 'test', type: 'msg_received', created_on: new Date().toISOString() };

// Mock setState and other dependencies to isolate the test
simulator.setState = jest.fn((newState, callback) => {
if (callback) callback();
});
simulator.updateEvents = jest.fn((events, session, callback) => callback());
simulator.updateActivity = jest.fn();
simulator.handleFocusUpdate = jest.fn();

// This should not throw when events is null
expect(() => {
simulator.updateRunContext(runContext, msgEvt);
}).not.toThrow();
});

it('handles undefined events array in runContext', () => {
const { instance } = setup();
const simulator = instance as any;

const runContext = {
contact: { uuid: 'test', urns: [], status: 'active', fields: {}, groups: [], created_on: '2020-01-01T00:00:00Z' },
session: { runs: [], contact: null, status: 'completed' },
context: {},
events: undefined, // This should cause the unshift error
segments: []
};

const msgEvt = { uuid: 'test', type: 'msg_received', created_on: new Date().toISOString() };

// Mock setState and other dependencies to isolate the test
simulator.setState = jest.fn((newState, callback) => {
if (callback) callback();
});
simulator.updateEvents = jest.fn((events, session, callback) => callback());
simulator.updateActivity = jest.fn();
simulator.handleFocusUpdate = jest.fn();

// This should not throw when events is undefined
expect(() => {
simulator.updateRunContext(runContext, msgEvt);
}).not.toThrow();
});
});

describe('resume error handling', () => {
it('handles error without response object', () => {
const { instance } = setup();
const simulator = instance as any;

// Mock setState to avoid component lifecycle issues
simulator.setState = jest.fn();
simulator.state = { events: [] };

// Create an error without response (network error)
const error = new Error('Network error');
// Don't set error.response - this should cause the error on line 521

// Test the catch block in resume method - simulate the actual catch logic
const catchHandler = (error: any) => {
if (error.response && error.response.status) {
// Condition check
}
const errorText = error.response && error.response.status > 499
? 'Server error, try again later'
: error.response && error.response.data && error.response.data.error
? error.response.data.error
: 'An error occurred';

return errorText;
};

// This should not throw when error.response is undefined
expect(() => {
const result = catchHandler(error);
expect(result).toBe('An error occurred');
}).not.toThrow();
});

it('handles error with response but no data', () => {
const { instance } = setup();
const simulator = instance as any;

simulator.setState = jest.fn();
simulator.state = { events: [] };

// Create an error with response but no data
const error = new Error('Server error');
(error as any).response = { status: 500 };

const catchHandler = (error: any) => {
if (error.response && error.response.status) {
// Condition check
}
const errorText = error.response && error.response.status > 499
? 'Server error, try again later'
: error.response && error.response.data && error.response.data.error
? error.response.data.error
: 'An error occurred';

return errorText;
};

expect(() => {
const result = catchHandler(error);
expect(result).toBe('Server error, try again later');
}).not.toThrow();
});
});
});
13 changes: 10 additions & 3 deletions src/components/simulator/Simulator.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,11 @@ export class Simulator extends React.Component<SimulatorProps, SimulatorState> {
}

private updateRunContext(runContext: RunContext, msgEvt?: any): void {
// Ensure events is always an array to prevent null/undefined errors
if (!runContext.events) {
runContext.events = [];
}

const wasJustActive = this.state.active || (runContext.events && runContext.events.length > 0);
this.setState({ quickReplies: [] }, () => {
// if session is waiting, find the wait event
Expand Down Expand Up @@ -518,16 +523,18 @@ export class Simulator extends React.Component<SimulatorProps, SimulatorState> {
this.updateRunContext(response.data as RunContext, msgInEvt);
})
.catch(error => {
if (error.response.status) {
if (error.response && error.response.status) {
}
const events = update(this.state.events, {
$push: [
{
type: 'error',
text:
error.response.status > 499
error.response && error.response.status > 499
? 'Server error, try again later'
: error.response.data.error
: error.response && error.response.data && error.response.data.error
? error.response.data.error
: 'An error occurred'
} as any
]
}) as EventProps[];
Expand Down