Skip to content

Commit

Permalink
Commit missing files
Browse files Browse the repository at this point in the history
  • Loading branch information
Zacqary committed Jun 18, 2020
1 parent 5f00cc1 commit bdf9b90
Showing 1 changed file with 62 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/

import { renderHook, act } from '@testing-library/react-hooks';

import { useWaffleOptions, WaffleOptionsState } from './use_waffle_options';

// Mock useUrlState hook
jest.mock('react-router-dom', () => ({
useHistory: () => ({
location: '',
replace: () => {},
}),
}));

// Jest can't access variables outside the scope of the mock factory function except to
// reassign them, so we can't make these both part of the same object
let PREFILL_NODETYPE;
let PREFILL_METRIC;
jest.mock('../../../../alerting/use_alert_prefill', () => ({
useAlertPrefillContext: () => ({
inventoryPrefill: {
setNodeType(nodeType: string) {
PREFILL_NODETYPE = nodeType;
},
setMetric(metric: { type: string }) {
PREFILL_METRIC = metric;
},
},
}),
}));

const renderUseWaffleOptionsHook = () => renderHook(() => useWaffleOptions());

describe('useWaffleOptions', () => {
beforeEach(() => {
PREFILL_NODETYPE = undefined;
PREFILL_METRIC = undefined;
});

it('should sync the options to the inventory alert preview context', () => {
const { result, rerender } = renderUseWaffleOptionsHook();

const newOptions = {
nodeType: 'pod',
metriic: { type: 'memory' },
} as WaffleOptionsState;
act(() => {
result.current.changeNodeType(newOptions.nodeType);
});
rerender();
expect(PREFILL_NODETYPE).toEqual(newOptions.nodeType);
act(() => {
result.current.changeMetric(newOptions.metric);
});
rerender();
expect(PREFILL_METRIC).toEqual(newOptions.metric);
});
});

0 comments on commit bdf9b90

Please sign in to comment.