From 008a003a4dc6c88c1f22bdd9739da0078cb4b004 Mon Sep 17 00:00:00 2001 From: Pascal Hartig Date: Fri, 6 Dec 2019 07:59:34 -0800 Subject: [PATCH] Add tests for healthchecks reducer Summary: Quite a bit of complex slicing going on there, so I think this is quite useful to guard against future breakages. Reviewed By: jknoxville Differential Revision: D18853033 fbshipit-source-id: 36b17b8ce208cb320a193bceed86c89e010107e4 --- src/reducers/__tests__/healthchecks.node.tsx | 91 ++++++++++++++++++++ 1 file changed, 91 insertions(+) create mode 100644 src/reducers/__tests__/healthchecks.node.tsx diff --git a/src/reducers/__tests__/healthchecks.node.tsx b/src/reducers/__tests__/healthchecks.node.tsx new file mode 100644 index 000000000..46b5d5437 --- /dev/null +++ b/src/reducers/__tests__/healthchecks.node.tsx @@ -0,0 +1,91 @@ +/** + * Copyright (c) Facebook, Inc. and its affiliates. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + * + * @format + */ + +import { + default as reducer, + initHealthcheckReport, + startHealthchecks, + HealthcheckReportCategory, + HealthcheckReportItem, + finishHealthchecks, + updateHealthcheckReportItem, +} from '../healthchecks'; + +const HEALTHCHECK_ITEM: HealthcheckReportItem = { + label: 'Test Check', + status: 'WARNING', + message: "Something didn't quite work.", +}; + +const HEALTHCHECK_CATEGORY: HealthcheckReportCategory = { + label: 'Test Category', + status: 'WARNING', + checks: [HEALTHCHECK_ITEM], +}; + +test('initHealthcheckReport', () => { + const report = { + isHealthcheckInProgress: false, + categories: [], + }; + const res = reducer(undefined, initHealthcheckReport(report)); + expect(res.healthcheckReport).toEqual(report); +}); + +test('startHealthCheck', () => { + const report = { + isHealthcheckInProgress: false, + categories: [HEALTHCHECK_CATEGORY], + }; + let res = reducer(undefined, initHealthcheckReport(report)); + res = reducer(res, startHealthchecks()); + expect(res.healthcheckReport.isHealthcheckInProgress).toBeTruthy(); + // This seems trivial, but by getting the spread wrong, it's easy + // to break this. + expect(res.healthcheckReport.categories).toEqual([HEALTHCHECK_CATEGORY]); +}); + +test('finish', () => { + const report = { + isHealthcheckInProgress: true, + categories: [HEALTHCHECK_CATEGORY], + }; + let res = reducer(undefined, initHealthcheckReport(report)); + res = reducer(res, finishHealthchecks()); + expect(res.healthcheckReport.isHealthcheckInProgress).toBeFalsy(); + expect(res.healthcheckReport.categories).toEqual([HEALTHCHECK_CATEGORY]); +}); + +test('updateHealthcheck', () => { + const report = { + isHealthcheckInProgress: true, + categories: [HEALTHCHECK_CATEGORY, HEALTHCHECK_CATEGORY], + }; + let res = reducer(undefined, initHealthcheckReport(report)); + res = reducer( + res, + updateHealthcheckReportItem(0, 0, { + label: 'Updated Test Item', + status: 'SUCCESS', + }), + ); + expect(res.healthcheckReport.isHealthcheckInProgress).toBeTruthy(); + expect(res.healthcheckReport.categories[0].checks[0].label).toEqual( + 'Updated Test Item', + ); + expect(res.healthcheckReport.categories[0].checks[0].status).toEqual( + 'SUCCESS', + ); + expect(res.healthcheckReport.categories[1].checks[0].label).toEqual( + 'Test Check', + ); + expect(res.healthcheckReport.categories[1].checks[0].status).toEqual( + 'WARNING', + ); +});