-
Notifications
You must be signed in to change notification settings - Fork 8.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
62 additions
and
0 deletions.
There are no files selected for viewing
62 changes: 62 additions & 0 deletions
62
x-pack/plugins/infra/public/pages/metrics/inventory_view/hooks/use_waffle_options.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); |