|
| 1 | +/** |
| 2 | + * Copyright (c) 2014-present, Facebook, Inc. All rights reserved. |
| 3 | + * |
| 4 | + * This source code is licensed under the MIT license found in the |
| 5 | + * LICENSE file in the root directory of this source tree. |
| 6 | + * |
| 7 | + */ |
| 8 | + |
| 9 | +'use strict'; |
| 10 | + |
| 11 | +import TestScheduler from '../test_scheduler'; |
| 12 | +import NotifyReporter from '../reporters/notify_reporter'; |
| 13 | +import type {TestSchedulerContext} from '../test_scheduler'; |
| 14 | + |
| 15 | +const SUCCESS_ICON_PATH = '/assets/jest_logo.png'; |
| 16 | +const FAILURE_ICON_PATH = 'TODO'; |
| 17 | + |
| 18 | +jest.mock('../reporters/default_reporter'); |
| 19 | +jest.mock('node-notifier', () => ({ |
| 20 | + notify: jest.fn(), |
| 21 | +})); |
| 22 | + |
| 23 | +const initialContext: TestSchedulerContext = { |
| 24 | + firstRun: true, |
| 25 | + previousSuccess: false, |
| 26 | +}; |
| 27 | + |
| 28 | +const aggregatedResultsSuccess: AggregatedResult = { |
| 29 | + numFailedTestSuites: 0, |
| 30 | + numFailedTests: 0, |
| 31 | + numPassedTestSuites: 1, |
| 32 | + numPassedTests: 3, |
| 33 | + numRuntimeErrorTestSuites: 0, |
| 34 | + numTotalTestSuites: 1, |
| 35 | + numTotalTests: 3, |
| 36 | + success: true, |
| 37 | +}; |
| 38 | + |
| 39 | +const iconDir = path => { |
| 40 | + if (path.endsWith(SUCCESS_ICON_PATH)) { |
| 41 | + return 'SUCCESS_ICON'; |
| 42 | + } else if (path.endsWith(FAILURE_ICON_PATH)) { |
| 43 | + return 'FAILURE_ICON'; |
| 44 | + } else { |
| 45 | + return 'ICON_NOT_FOUND'; |
| 46 | + } |
| 47 | +}; |
| 48 | + |
| 49 | +test('.addReporter() .removeReporter()', () => { |
| 50 | + const scheduler = new TestScheduler({}, {}, initialContext); |
| 51 | + const reporter = new NotifyReporter(); |
| 52 | + scheduler.addReporter(reporter); |
| 53 | + expect(scheduler._dispatcher._reporters).toContain(reporter); |
| 54 | + scheduler.removeReporter(NotifyReporter); |
| 55 | + expect(scheduler._dispatcher._reporters).not.toContain(reporter); |
| 56 | +}); |
| 57 | + |
| 58 | +test('test always', () => { |
| 59 | + const notify = require('node-notifier'); |
| 60 | + |
| 61 | + const reporter = new NotifyReporter( |
| 62 | + {notify: true, notifyMode: 'always'}, |
| 63 | + {}, |
| 64 | + initialContext, |
| 65 | + ); |
| 66 | + reporter.onRunComplete(new Set(), aggregatedResultsSuccess); |
| 67 | + expect( |
| 68 | + notify.notify.mock.calls.map(([{icon, message, title}]) => ({ |
| 69 | + icon: iconDir(icon), |
| 70 | + message, |
| 71 | + title, |
| 72 | + })), |
| 73 | + ).toMatchSnapshot(); |
| 74 | +}); |
0 commit comments