-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathStack.test.tsx
More file actions
155 lines (117 loc) · 4.54 KB
/
Stack.test.tsx
File metadata and controls
155 lines (117 loc) · 4.54 KB
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
import '@testing-library/jest-dom';
import { render, screen } from '@testing-library/react';
import React from 'react';
import {
ariaAttributesTest,
classNamePrefixProviderTest,
elementTypePropsTest,
restPropsTest,
stylePropsTest,
validHtmlAttributesTest,
} from '@local/tests';
import Stack from '../Stack';
import StackItem from '../StackItem';
describe('Stack', () => {
classNamePrefixProviderTest(Stack, 'Stack');
stylePropsTest(Stack);
restPropsTest(Stack, 'div');
validHtmlAttributesTest(Stack);
ariaAttributesTest(Stack);
elementTypePropsTest(Stack);
it('should render text children', () => {
render(<Stack data-testid="stack">Hello World</Stack>);
expect(screen.getByTestId('stack')).toHaveTextContent('Hello World');
});
it('should render element children', () => {
render(
<Stack data-testid="stack">
<span>Child 1</span>
<span>Child 2</span>
</Stack>,
);
const element = screen.getByTestId('stack');
expect(element.children).toHaveLength(2);
expect(element.children[0]).toHaveTextContent('Child 1');
expect(element.children[1]).toHaveTextContent('Child 2');
});
it('should render with spacing', () => {
render(<Stack data-testid="stack" hasSpacing />);
expect(screen.getByTestId('stack')).toHaveClass('Stack--hasSpacing');
});
it('should render with custom spacing', () => {
render(<Stack data-testid="stack" spacing="space-1000" />);
const element = screen.getByTestId('stack');
expect(element).toHaveClass('Stack--hasSpacing');
expect(element).toHaveStyle({ '--stack-spacing': 'var(--spirit-space-1000)' });
});
it('should render with custom spacing for each breakpoint', () => {
render(
<Stack data-testid="stack" spacing={{ mobile: 'space-100', tablet: 'space-1000', desktop: 'space-1200' }} />,
);
const element = screen.getByTestId('stack');
expect(element).toHaveClass('Stack--hasSpacing');
expect(element).toHaveStyle({ '--stack-spacing': 'var(--spirit-space-100)' });
expect(element).toHaveStyle({ '--stack-spacing-tablet': 'var(--spirit-space-1000)' });
expect(element).toHaveStyle({ '--stack-spacing-desktop': 'var(--spirit-space-1200)' });
});
it('should render with custom spacing for only one breakpoint', () => {
render(<Stack data-testid="stack" spacing={{ tablet: 'space-1000' }} />);
const element = screen.getByTestId('stack');
expect(element).toHaveClass('Stack--hasSpacing');
expect(element).toHaveStyle({ '--stack-spacing-tablet': 'var(--spirit-space-1000)' });
expect(element).not.toHaveStyle({ '--stack-spacing': 'var(--spirit-space-100)' });
expect(element).not.toHaveStyle({ '--stack-spacing-desktop': 'var(--spirit-space-1200)' });
});
it('should provide context so StackItem renders as li when Stack elementType is ul', () => {
render(
<Stack elementType="ul">
<StackItem data-testid="item">Item</StackItem>
</Stack>,
);
const list = screen.getByRole('list');
expect(list.localName).toBe('ul');
const item = screen.getByTestId('item');
expect(item).toBeInTheDocument();
expect(item.localName).toBe('li');
expect(item).toHaveTextContent('Item');
});
it('should provide context so StackItem renders as li when Stack elementType is ol', () => {
render(
<Stack elementType="ol">
<StackItem data-testid="item">Item</StackItem>
</Stack>,
);
const list = screen.getByRole('list');
expect(list.localName).toBe('ol');
const item = screen.getByTestId('item');
expect(item).toBeInTheDocument();
expect(item.localName).toBe('li');
expect(item).toHaveTextContent('Item');
});
it('should allow explicit StackItem elementType to override context', () => {
render(
<Stack elementType="ul">
<StackItem elementType="div" data-testid="item">
Item
</StackItem>
</Stack>,
);
expect(screen.getByRole('list')).toBeInTheDocument();
const item = screen.getByTestId('item');
expect(item).toBeInTheDocument();
expect(item.localName).toBe('div');
expect(screen.queryByRole('listitem')).not.toBeInTheDocument();
});
it('should prevent nested StackItem elements (or any other element) from inheriting context', () => {
render(
<Stack elementType="ul">
<StackItem data-testid="item">
<Stack>
<StackItem data-testid="nested-item">Item</StackItem>
</Stack>
</StackItem>
</Stack>,
);
expect(screen.queryByTestId('nested-item')?.localName).toBe('div');
});
});