Debian Codegraph @

node/@patternfly/react-core/src/helpers/__tests__/util.test.ts

raw
1 import {
2 capitalize,
3 getUniqueId,
4 debounce,
5 isElementInView,
6 sideElementIsOutOfView,
7 fillTemplate,
8 pluralize,
9 formatBreakpointMods
10 } from '../util';
11 import { SIDE } from '../constants';
12 import styles from '@patternfly/react-styles/css/layouts/Flex/flex';
13
14 const createMockHtmlElement = (bounds: Partial<DOMRect>) =>
15 ({
16 getBoundingClientRect: () => bounds
17 }) as HTMLElement;
18
19 test('capitalize', () => {
20 expect(capitalize('foo')).toBe('Foo');
21 });
22
23 test('getUniqueId', () => {
24 expect(getUniqueId()).not.toBe(getUniqueId());
25 });
26
27 test('getUniqueId prefixed', () => {
28 expect(getUniqueId().substring(0, 3)).toBe('pf-');
29 expect(getUniqueId('pf-switch').substring(0, 10)).toBe('pf-switch-');
30 });
31
32 test('debounce', () => {
33 jest.useFakeTimers();
34 const callback = jest.fn();
35 const debouncedFunction = debounce(callback, 50);
36
37 debouncedFunction();
38 // At this point in time, the callback should not have been called yet
39 expect(callback).toHaveBeenCalledTimes(0);
40
41 for (let i = 0; i < 10; i++) {
42 jest.advanceTimersByTime(50);
43 debouncedFunction();
44 }
45
46 expect(callback).toHaveBeenCalledTimes(10);
47 });
48
49 test('isElementInView should be true when partial out of view and with partial true', () => {
50 const container = createMockHtmlElement({ left: 0, right: 200 });
51 const element = createMockHtmlElement({ left: 10, right: 210 });
52 expect(isElementInView(container, element, true)).toBe(true);
53 });
54
55 test('isElementInView should be false when partial out of view and with partial false ', () => {
56 const container = createMockHtmlElement({ left: 0, right: 200 });
57 const element = createMockHtmlElement({ left: 10, right: 210 });
58 expect(isElementInView(container, element, false)).toBe(false);
59 });
60
61 test('isElementInView should be false completely out of view ', () => {
62 const container = createMockHtmlElement({ left: 0, right: 200 });
63 const element = createMockHtmlElement({ left: 200, right: 300 });
64 expect(isElementInView(container, element, true)).toBe(false);
65 });
66
67 test('isElementInView should be false completely out of view when partial false ', () => {
68 const container = createMockHtmlElement({ left: 0, right: 200 });
69 const element = createMockHtmlElement({ left: 200, right: 300 });
70 expect(isElementInView(container, element, false)).toBe(false);
71 });
72
73 test('sideElementIsOutOfView Returns left when off on left side', () => {
74 const container = createMockHtmlElement({ left: 20, right: 220 });
75 const element = createMockHtmlElement({ left: 10, right: 210 });
76 expect(sideElementIsOutOfView(container, element)).toBe(SIDE.LEFT);
77 });
78
79 test('sideElementIsOutOfView Returns right when off on right side', () => {
80 const container = createMockHtmlElement({ left: 0, right: 200 });
81 const element = createMockHtmlElement({ left: 210, right: 410 });
82 expect(sideElementIsOutOfView(container, element)).toBe(SIDE.RIGHT);
83 });
84
85 test('sideElementIsOutOfView Returns NONE when in view', () => {
86 const container = createMockHtmlElement({ left: 0, right: 200 });
87 const element = createMockHtmlElement({ left: 10, right: 110 });
88 expect(sideElementIsOutOfView(container, element)).toBe(SIDE.NONE);
89 });
90
91 test('fillTemplate interpolates strings correctly', () => {
92 const templateString = 'My name is ${firstName} ${lastName}';
93 const expected = 'My name is Jon Dough';
94 const templatVars = {
95 firstName: 'Jon',
96 lastName: 'Dough'
97 };
98 const actual = fillTemplate(templateString, templatVars);
99 expect(actual).toEqual(expected);
100 });
101
102 test('text pluralize', () => {
103 expect(pluralize(1, 'dog')).toEqual('1 dog');
104 expect(pluralize(2, 'dog')).toEqual('2 dogs');
105 expect(pluralize(2, 'finch', 'finches')).toEqual('2 finches');
106 });
107
108 test('formatBreakpointMods', () => {
109 expect(formatBreakpointMods({ default: 'spacerNone' }, styles)).toEqual('pf-m-spacer-none');
110 expect(formatBreakpointMods({ md: 'spacerNone' }, styles)).toEqual('pf-m-spacer-none-on-md');
111 expect(formatBreakpointMods({ default: 'column', lg: 'row' }, styles)).toEqual('pf-m-column pf-m-row-on-lg');
112 });