|
1 | 1 | import 'dart:html'; |
| 2 | +import 'package:logging/logging.dart'; |
2 | 3 | import 'package:over_react/over_react.dart'; |
3 | 4 | import 'package:over_react_test/over_react_test.dart'; |
4 | 5 | import 'package:test/test.dart'; |
@@ -704,6 +705,164 @@ void sharedErrorBoundaryTests(BuilderOnlyUiFactory builder) { |
704 | 705 | }); |
705 | 706 | }); |
706 | 707 |
|
| 708 | + group('logs errors using a `logger`', () { |
| 709 | + List<Map<String, List>> calls; |
| 710 | + List<LogRecord> logRecords; |
| 711 | + const identicalErrorFrequencyToleranceInMs = 500; |
| 712 | + |
| 713 | + // Cause an error to be thrown within a ReactJS lifecycle method |
| 714 | + void triggerAComponentError() { |
| 715 | + queryByTestId(jacket.getInstance(), 'flawedComponent_flawedButton').click(); |
| 716 | + } |
| 717 | + |
| 718 | + void sharedSetup({String loggerName, bool shouldLogErrors = true, Logger customLogger}) { |
| 719 | + calls = []; |
| 720 | + jacket = mount( |
| 721 | + (ErrorBoundary() |
| 722 | + ..loggerName = loggerName |
| 723 | + ..shouldLogErrors = shouldLogErrors |
| 724 | + ..logger = customLogger |
| 725 | + ..identicalErrorFrequencyTolerance = const Duration(milliseconds: identicalErrorFrequencyToleranceInMs) |
| 726 | + ..onComponentDidCatch = (err, info) { |
| 727 | + calls.add({'onComponentDidCatch': [err, info]}); |
| 728 | + } |
| 729 | + ..onComponentIsUnrecoverable = (err, info) { |
| 730 | + calls.add({'onComponentIsUnrecoverable': [err, info]}); |
| 731 | + } |
| 732 | + )(Flawed()()), |
| 733 | + attachedToDocument: true, |
| 734 | + ); |
| 735 | + |
| 736 | + logRecords = []; |
| 737 | + final subscription = |
| 738 | + Logger(customLogger?.name ?? loggerName ?? defaultErrorBoundaryLoggerName).onRecord.listen(logRecords.add); |
| 739 | + addTearDown(subscription.cancel); |
| 740 | + } |
| 741 | + |
| 742 | + tearDown(() { |
| 743 | + logRecords = null; |
| 744 | + }); |
| 745 | + |
| 746 | + test('when `props.shouldLogErrors` is false', () { |
| 747 | + sharedSetup(shouldLogErrors: false); |
| 748 | + triggerAComponentError(); |
| 749 | + expect(logRecords, isEmpty); |
| 750 | + }); |
| 751 | + |
| 752 | + group('provided via `props.logger`', () { |
| 753 | + test('', () { |
| 754 | + sharedSetup(customLogger: Logger('myCustomLoggerLoggerName')); |
| 755 | + triggerAComponentError(); |
| 756 | + |
| 757 | + expect(logRecords, hasLength(1)); |
| 758 | + expect(logRecords.single.level, Level.SEVERE); |
| 759 | + expect(logRecords.single.loggerName, 'myCustomLoggerLoggerName'); |
| 760 | + expect(logRecords.single.error, calls.single['onComponentDidCatch'][0]); |
| 761 | + |
| 762 | + ReactErrorInfo infoSentToCallback = calls.single['onComponentDidCatch'][1]; |
| 763 | + expect(logRecords.single.stackTrace, infoSentToCallback.dartStackTrace); |
| 764 | + expect(logRecords.single.message, 'An error was caught by an ErrorBoundary:' |
| 765 | + ' \nInfo: ${infoSentToCallback.componentStack}'); |
| 766 | + }); |
| 767 | + |
| 768 | + test('and `props.loggerName` is also set', () { |
| 769 | + sharedSetup(loggerName: 'somethingElse', customLogger: Logger('myCustomLoggerLoggerName')); |
| 770 | + triggerAComponentError(); |
| 771 | + |
| 772 | + expect(logRecords.single.loggerName, 'myCustomLoggerLoggerName'); |
| 773 | + }); |
| 774 | + }); |
| 775 | + |
| 776 | + group('when `props.loggerName` is not set', () { |
| 777 | + setUp(sharedSetup); |
| 778 | + |
| 779 | + test('and a component error is caught', () { |
| 780 | + triggerAComponentError(); |
| 781 | + |
| 782 | + expect(logRecords, hasLength(1)); |
| 783 | + expect(logRecords.single.level, Level.SEVERE); |
| 784 | + expect(logRecords.single.loggerName, defaultErrorBoundaryLoggerName); |
| 785 | + expect(logRecords.single.error, calls.single['onComponentDidCatch'][0]); |
| 786 | + |
| 787 | + ReactErrorInfo infoSentToCallback = calls.single['onComponentDidCatch'][1]; |
| 788 | + expect(logRecords.single.stackTrace, infoSentToCallback.dartStackTrace); |
| 789 | + expect(logRecords.single.message, 'An error was caught by an ErrorBoundary:' |
| 790 | + ' \nInfo: ${infoSentToCallback.componentStack}'); |
| 791 | + }); |
| 792 | + |
| 793 | + test('and an unrecoverable component error is caught', () async { |
| 794 | + triggerAComponentError(); |
| 795 | + await Future.delayed(const Duration(milliseconds: identicalErrorFrequencyToleranceInMs ~/ 2)); |
| 796 | + triggerAComponentError(); |
| 797 | + |
| 798 | + expect(logRecords, hasLength(2)); |
| 799 | + expect(logRecords[1].level, Level.SEVERE); |
| 800 | + expect(logRecords[1].loggerName, defaultErrorBoundaryLoggerName); |
| 801 | + expect(logRecords[1].error, calls[2]['onComponentIsUnrecoverable'][0]); |
| 802 | + |
| 803 | + ReactErrorInfo infoSentToCallback = calls[2]['onComponentIsUnrecoverable'][1]; |
| 804 | + expect(logRecords[1].stackTrace, infoSentToCallback.dartStackTrace); |
| 805 | + expect(logRecords[1].message, |
| 806 | + 'An unrecoverable error was caught by an ErrorBoundary (attempting to remount it was unsuccessful):' |
| 807 | + ' \nInfo: ${infoSentToCallback.componentStack}'); |
| 808 | + }); |
| 809 | + |
| 810 | + group('but then `props.loggerName` is set to a non-null value', () { |
| 811 | + setUp(() { |
| 812 | + Logger('myCustomErrorLoggerName').clearListeners(); |
| 813 | + jacket.rerender( |
| 814 | + (ErrorBoundary() |
| 815 | + ..loggerName = 'myCustomErrorLoggerName' |
| 816 | + ..identicalErrorFrequencyTolerance = const Duration(milliseconds: identicalErrorFrequencyToleranceInMs) |
| 817 | + ..onComponentDidCatch = (err, info) { |
| 818 | + calls.add({'onComponentDidCatch': [err, info]}); |
| 819 | + } |
| 820 | + ..onComponentIsUnrecoverable = (err, info) { |
| 821 | + calls.add({'onComponentIsUnrecoverable': [err, info]}); |
| 822 | + } |
| 823 | + )(Flawed()()) |
| 824 | + ); |
| 825 | + final subscription = Logger('myCustomErrorLoggerName').onRecord.listen(logRecords.add); |
| 826 | + addTearDown(subscription.cancel); |
| 827 | + }); |
| 828 | + |
| 829 | + test('and a component error is caught', () { |
| 830 | + triggerAComponentError(); |
| 831 | + |
| 832 | + expect(logRecords.single.loggerName, 'myCustomErrorLoggerName'); |
| 833 | + }); |
| 834 | + |
| 835 | + group('and then to a null value', () { |
| 836 | + setUp(() { |
| 837 | + Logger(defaultErrorBoundaryLoggerName).clearListeners(); |
| 838 | + jacket.rerender( |
| 839 | + (ErrorBoundary() |
| 840 | + ..loggerName = null |
| 841 | + ..identicalErrorFrequencyTolerance = const Duration(milliseconds: identicalErrorFrequencyToleranceInMs) |
| 842 | + ..onComponentDidCatch = (err, info) { |
| 843 | + calls.add({'onComponentDidCatch': [err, info]}); |
| 844 | + } |
| 845 | + ..onComponentIsUnrecoverable = (err, info) { |
| 846 | + calls.add({'onComponentIsUnrecoverable': [err, info]}); |
| 847 | + } |
| 848 | + )(Flawed()()) |
| 849 | + ); |
| 850 | + final subscription = Logger(defaultErrorBoundaryLoggerName).onRecord.listen(logRecords.add); |
| 851 | + addTearDown(subscription.cancel); |
| 852 | + }); |
| 853 | + |
| 854 | + test('and a component error is caught', () { |
| 855 | + triggerAComponentError(); |
| 856 | + |
| 857 | + expect(logRecords.single.loggerName, defaultErrorBoundaryLoggerName, |
| 858 | + reason: 'The loggerName should fall back to `defaultErrorBoundaryLoggerName` ' |
| 859 | + 'if a consumer attempts to set it to null'); |
| 860 | + }); |
| 861 | + }); |
| 862 | + }); |
| 863 | + }); |
| 864 | + }); |
| 865 | + |
707 | 866 | // group('throws a PropError when', () { |
708 | 867 | // test('more than one child is provided', () { |
709 | 868 | // expect(() => mount(builder()(dummyChild, dummyChild)), |
|
0 commit comments