forked from ericgio/react-bootstrap-typeahead
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOverlay.test.tsx
113 lines (91 loc) · 3.04 KB
/
Overlay.test.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
import React from 'react';
import { getModifiers, getPlacement } from './useOverlay';
import * as stories from './Overlay.stories';
import { Align } from '../../types';
import {
composeStories,
generateSnapshots,
getMenu,
render,
screen,
waitFor,
} from '../../tests/helpers';
const { Default } = composeStories(stories);
interface PlacementPermutation {
props: {
align: Align;
dropup: boolean;
};
received: string;
}
interface Modifier {
name: string;
}
interface ModifierProps {
align: Align;
flip: boolean;
}
describe('<Overlay>', () => {
generateSnapshots(stories);
it('renders the overlay', async () => {
render(<Default />);
await waitFor(() => {
expect(getMenu()).toBeInTheDocument();
});
});
it('does not render the overlay when `isMenuShown=false`', () => {
render(<Default isMenuShown={false} />);
expect(screen.queryByRole('listbox')).not.toBeInTheDocument();
});
it('updates the positioning type', async () => {
// The story uses fixed positioning by default.
const { rerender } = render(<Default />);
await waitFor(() => {
expect(getMenu()).toHaveStyle('position: fixed');
});
rerender(<Default positionFixed={false} />);
await waitFor(() => {
expect(getMenu()).toHaveStyle('position: absolute');
});
});
});
describe('Overlay placement', () => {
it('computes the placement string', () => {
const permutations: PlacementPermutation[] = [
{ props: { align: 'right', dropup: false }, received: 'bottom-end' },
{ props: { align: 'left', dropup: false }, received: 'bottom-start' },
{ props: { align: 'justify', dropup: false }, received: 'bottom-start' },
// @ts-expect-error
{ props: { align: 'foo', dropup: false }, received: 'bottom-start' },
{ props: { align: 'right', dropup: true }, received: 'top-end' },
{ props: { align: 'left', dropup: true }, received: 'top-start' },
{ props: { align: 'justify', dropup: true }, received: 'top-start' },
// @ts-expect-error
{ props: { align: 'foo', dropup: true }, received: 'top-start' },
];
permutations.forEach(({ props, received }) => {
expect(getPlacement(props)).toBe(received);
});
});
});
describe('Overlay modifiers', () => {
it('sets the `flip` modifier', () => {
const props: ModifierProps = { align: 'justify', flip: false };
const selector = ({ name }: Modifier) => name === 'flip';
expect(getModifiers(props).find(selector)?.enabled).toBe(false);
props.flip = true;
expect(getModifiers(props).find(selector)?.enabled).toBe(true);
});
it('conditionally adds the `setWidth` modifier', () => {
const props: ModifierProps = { align: 'justify', flip: false };
const modifiers = getModifiers(props);
expect(modifiers).toHaveLength(3);
expect(
modifiers.find(({ name }) => name === 'setPopperWidth')
).toBeTruthy();
props.align = 'left';
expect(getModifiers(props)).toHaveLength(2);
props.align = 'right';
expect(getModifiers(props)).toHaveLength(2);
});
});