Skip to content

Commit 5e12bb5

Browse files
committed
test: enhance unit tests for IterableInboxMessageDisplay with additional scenarios
1 parent cd76d07 commit 5e12bb5

1 file changed

Lines changed: 208 additions & 0 deletions

File tree

src/inbox/components/IterableInboxMessageDisplay.test.tsx

Lines changed: 208 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -419,6 +419,214 @@ describe('IterableInboxMessageDisplay', () => {
419419

420420
expect(mockReturnToInbox).toHaveBeenCalledWith(expect.any(Function));
421421
});
422+
423+
// Additional comprehensive tests for the specific lines highlighted
424+
it('should execute deleteRow callback when delete action is triggered', async () => {
425+
const mockReturnToInbox = jest.fn();
426+
const mockDeleteRow = jest.fn();
427+
const propsWithMocks = {
428+
...defaultProps,
429+
returnToInbox: mockReturnToInbox,
430+
deleteRow: mockDeleteRow,
431+
};
432+
433+
const { getByTestId } = render(<IterableInboxMessageDisplay {...propsWithMocks} />);
434+
435+
await waitFor(() => {
436+
expect(getByTestId('webview')).toBeTruthy();
437+
});
438+
439+
const deleteTrigger = getByTestId('webview-delete-trigger');
440+
fireEvent.press(deleteTrigger);
441+
442+
// Verify returnToInbox is called with a callback
443+
expect(mockReturnToInbox).toHaveBeenCalledWith(expect.any(Function));
444+
445+
// Execute the callback to verify deleteRow is called with correct messageId
446+
const callback = mockReturnToInbox.mock.calls[0][0];
447+
callback();
448+
expect(mockDeleteRow).toHaveBeenCalledWith('test-message-id');
449+
});
450+
451+
it('should call returnToInbox without callback for dismiss action', async () => {
452+
const mockReturnToInbox = jest.fn();
453+
const propsWithMockReturn = {
454+
...defaultProps,
455+
returnToInbox: mockReturnToInbox,
456+
};
457+
458+
const { getByTestId } = render(<IterableInboxMessageDisplay {...propsWithMockReturn} />);
459+
460+
await waitFor(() => {
461+
expect(getByTestId('webview')).toBeTruthy();
462+
});
463+
464+
const dismissTrigger = getByTestId('webview-dismiss-trigger');
465+
fireEvent.press(dismissTrigger);
466+
467+
// Verify returnToInbox is called without any arguments (no callback)
468+
expect(mockReturnToInbox).toHaveBeenCalledWith();
469+
});
470+
471+
it('should call Linking.openURL for HTTP URLs', async () => {
472+
const mockReturnToInbox = jest.fn();
473+
const { Linking } = require('react-native');
474+
const propsWithMockReturn = {
475+
...defaultProps,
476+
returnToInbox: mockReturnToInbox,
477+
};
478+
479+
const { getByTestId } = render(<IterableInboxMessageDisplay {...propsWithMockReturn} />);
480+
481+
await waitFor(() => {
482+
expect(getByTestId('webview')).toBeTruthy();
483+
});
484+
485+
const messageTrigger = getByTestId('webview-message-trigger');
486+
fireEvent.press(messageTrigger);
487+
488+
// Verify returnToInbox is called with a callback
489+
expect(mockReturnToInbox).toHaveBeenCalledWith(expect.any(Function));
490+
491+
// Execute the callback to verify Linking.openURL is called
492+
const callback = mockReturnToInbox.mock.calls[0][0];
493+
callback();
494+
expect(Linking.openURL).toHaveBeenCalledWith('https://example.com');
495+
});
496+
497+
it('should call customActionHandler with correct action and context', async () => {
498+
const mockReturnToInbox = jest.fn();
499+
const { Iterable } = require('../../core/classes/Iterable');
500+
const mockCustomActionHandler = jest.fn();
501+
Iterable.savedConfig.customActionHandler = mockCustomActionHandler;
502+
503+
const propsWithMockReturn = {
504+
...defaultProps,
505+
returnToInbox: mockReturnToInbox,
506+
};
507+
508+
const { getByTestId } = render(<IterableInboxMessageDisplay {...propsWithMockReturn} />);
509+
510+
await waitFor(() => {
511+
expect(getByTestId('webview')).toBeTruthy();
512+
});
513+
514+
const customActionTrigger = getByTestId('webview-custom-action-trigger');
515+
fireEvent.press(customActionTrigger);
516+
517+
// Verify returnToInbox is called with a callback
518+
expect(mockReturnToInbox).toHaveBeenCalledWith(expect.any(Function));
519+
520+
// Execute the callback to verify customActionHandler is called
521+
const callback = mockReturnToInbox.mock.calls[0][0];
522+
callback();
523+
expect(mockCustomActionHandler).toHaveBeenCalledWith(
524+
expect.objectContaining({
525+
type: 'customAction',
526+
data: 'action://customAction',
527+
userInput: '',
528+
}),
529+
expect.objectContaining({
530+
action: expect.objectContaining({
531+
type: 'customAction',
532+
data: 'action://customAction',
533+
userInput: '',
534+
}),
535+
source: 2, // IterableActionSource.inApp
536+
})
537+
);
538+
});
539+
540+
it('should call urlHandler with correct URL and context for deep links', async () => {
541+
const mockReturnToInbox = jest.fn();
542+
const { Iterable } = require('../../core/classes/Iterable');
543+
const mockUrlHandler = jest.fn();
544+
Iterable.savedConfig.urlHandler = mockUrlHandler;
545+
546+
const propsWithMockReturn = {
547+
...defaultProps,
548+
returnToInbox: mockReturnToInbox,
549+
};
550+
551+
const { getByTestId } = render(<IterableInboxMessageDisplay {...propsWithMockReturn} />);
552+
553+
await waitFor(() => {
554+
expect(getByTestId('webview')).toBeTruthy();
555+
});
556+
557+
const deepLinkTrigger = getByTestId('webview-deep-link-trigger');
558+
fireEvent.press(deepLinkTrigger);
559+
560+
// Verify returnToInbox is called with a callback
561+
expect(mockReturnToInbox).toHaveBeenCalledWith(expect.any(Function));
562+
563+
// Execute the callback to verify urlHandler is called
564+
const callback = mockReturnToInbox.mock.calls[0][0];
565+
callback();
566+
expect(mockUrlHandler).toHaveBeenCalledWith(
567+
'myapp://deep-link',
568+
expect.objectContaining({
569+
action: expect.objectContaining({
570+
type: 'openUrl',
571+
data: 'myapp://deep-link',
572+
userInput: '',
573+
}),
574+
source: 2, // IterableActionSource.inApp
575+
})
576+
);
577+
});
578+
579+
it('should handle missing customActionHandler gracefully', async () => {
580+
const mockReturnToInbox = jest.fn();
581+
const { Iterable } = require('../../core/classes/Iterable');
582+
// Set customActionHandler to undefined
583+
Iterable.savedConfig.customActionHandler = undefined;
584+
585+
const propsWithMockReturn = {
586+
...defaultProps,
587+
returnToInbox: mockReturnToInbox,
588+
};
589+
590+
const { getByTestId } = render(<IterableInboxMessageDisplay {...propsWithMockReturn} />);
591+
592+
await waitFor(() => {
593+
expect(getByTestId('webview')).toBeTruthy();
594+
});
595+
596+
const customActionTrigger = getByTestId('webview-custom-action-trigger');
597+
598+
// Should not throw an error even when customActionHandler is undefined
599+
expect(() => fireEvent.press(customActionTrigger)).not.toThrow();
600+
601+
// Verify returnToInbox is still called
602+
expect(mockReturnToInbox).toHaveBeenCalledWith(expect.any(Function));
603+
});
604+
605+
it('should handle missing urlHandler gracefully', async () => {
606+
const mockReturnToInbox = jest.fn();
607+
const { Iterable } = require('../../core/classes/Iterable');
608+
// Set urlHandler to undefined
609+
Iterable.savedConfig.urlHandler = undefined;
610+
611+
const propsWithMockReturn = {
612+
...defaultProps,
613+
returnToInbox: mockReturnToInbox,
614+
};
615+
616+
const { getByTestId } = render(<IterableInboxMessageDisplay {...propsWithMockReturn} />);
617+
618+
await waitFor(() => {
619+
expect(getByTestId('webview')).toBeTruthy();
620+
});
621+
622+
const deepLinkTrigger = getByTestId('webview-deep-link-trigger');
623+
624+
// Should not throw an error even when urlHandler is undefined
625+
expect(() => fireEvent.press(deepLinkTrigger)).not.toThrow();
626+
627+
// Verify returnToInbox is still called
628+
expect(mockReturnToInbox).toHaveBeenCalledWith(expect.any(Function));
629+
});
422630
});
423631

424632
describe('Tracking and Analytics', () => {

0 commit comments

Comments
 (0)