@instructure/ui-truncate-text 10.11.0 → 10.11.1-snapshot-2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +8 -0
- package/es/TruncateText/__new-tests__/TruncateText.test.js +75 -0
- package/es/TruncateText/utils/__new-tests__/cleanData.test.js +108 -0
- package/es/TruncateText/utils/__new-tests__/cleanString.test.js +49 -0
- package/es/TruncateText/utils/__new-tests__/measureText.test.js +148 -0
- package/lib/TruncateText/__new-tests__/TruncateText.test.js +77 -0
- package/lib/TruncateText/utils/__new-tests__/cleanData.test.js +112 -0
- package/lib/TruncateText/utils/__new-tests__/cleanString.test.js +52 -0
- package/lib/TruncateText/utils/__new-tests__/measureText.test.js +150 -0
- package/package.json +19 -15
- package/src/TruncateText/__new-tests__/TruncateText.test.tsx +99 -0
- package/src/TruncateText/utils/__new-tests__/cleanData.test.tsx +131 -0
- package/src/TruncateText/utils/__new-tests__/cleanString.test.tsx +57 -0
- package/src/TruncateText/utils/__new-tests__/measureText.test.tsx +173 -0
- package/tsconfig.build.json +1 -0
- package/tsconfig.build.tsbuildinfo +1 -1
- package/types/TruncateText/__new-tests__/TruncateText.test.d.ts +2 -0
- package/types/TruncateText/__new-tests__/TruncateText.test.d.ts.map +1 -0
- package/types/TruncateText/utils/__new-tests__/cleanData.test.d.ts +2 -0
- package/types/TruncateText/utils/__new-tests__/cleanData.test.d.ts.map +1 -0
- package/types/TruncateText/utils/__new-tests__/cleanString.test.d.ts +2 -0
- package/types/TruncateText/utils/__new-tests__/cleanString.test.d.ts.map +1 -0
- package/types/TruncateText/utils/__new-tests__/measureText.test.d.ts +2 -0
- package/types/TruncateText/utils/__new-tests__/measureText.test.d.ts.map +1 -0
package/CHANGELOG.md
CHANGED
|
@@ -3,6 +3,14 @@
|
|
|
3
3
|
All notable changes to this project will be documented in this file.
|
|
4
4
|
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
|
|
5
5
|
|
|
6
|
+
## [10.11.1-snapshot-2](https://github.com/instructure/instructure-ui/compare/v10.11.0...v10.11.1-snapshot-2) (2025-02-05)
|
|
7
|
+
|
|
8
|
+
**Note:** Version bump only for package @instructure/ui-truncate-text
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
|
|
6
14
|
# [10.11.0](https://github.com/instructure/instructure-ui/compare/v10.10.0...v10.11.0) (2025-02-03)
|
|
7
15
|
|
|
8
16
|
**Note:** Version bump only for package @instructure/ui-truncate-text
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
var _TruncateText, _TruncateText2, _TruncateText3, _TruncateText4, _TruncateText5;
|
|
2
|
+
/*
|
|
3
|
+
* The MIT License (MIT)
|
|
4
|
+
*
|
|
5
|
+
* Copyright (c) 2015 - present Instructure, Inc.
|
|
6
|
+
*
|
|
7
|
+
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
8
|
+
* of this software and associated documentation files (the "Software"), to deal
|
|
9
|
+
* in the Software without restriction, including without limitation the rights
|
|
10
|
+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
11
|
+
* copies of the Software, and to permit persons to whom the Software is
|
|
12
|
+
* furnished to do so, subject to the following conditions:
|
|
13
|
+
*
|
|
14
|
+
* The above copyright notice and this permission notice shall be included in all
|
|
15
|
+
* copies or substantial portions of the Software.
|
|
16
|
+
*
|
|
17
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
18
|
+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
19
|
+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
20
|
+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
21
|
+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
22
|
+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
23
|
+
* SOFTWARE.
|
|
24
|
+
*/
|
|
25
|
+
|
|
26
|
+
import React from 'react';
|
|
27
|
+
import { render, waitFor } from '@testing-library/react';
|
|
28
|
+
import { vi, expect } from 'vitest';
|
|
29
|
+
import '@testing-library/jest-dom';
|
|
30
|
+
import { runAxeCheck } from '@instructure/ui-axe-check';
|
|
31
|
+
import { TruncateText } from '../index';
|
|
32
|
+
const defaultText = 'Hello world! This is a long string that should truncate';
|
|
33
|
+
describe('<TruncateText />', () => {
|
|
34
|
+
let consoleErrorMock;
|
|
35
|
+
beforeEach(() => {
|
|
36
|
+
// Mocking console to prevent test output pollution and expect for messages
|
|
37
|
+
consoleErrorMock = vi.spyOn(console, 'error').mockImplementation(() => {});
|
|
38
|
+
});
|
|
39
|
+
afterEach(() => {
|
|
40
|
+
consoleErrorMock.mockRestore();
|
|
41
|
+
});
|
|
42
|
+
it('should warn if children prop receives too deep of a node tree', async () => {
|
|
43
|
+
render(/*#__PURE__*/React.createElement("div", {
|
|
44
|
+
style: {
|
|
45
|
+
width: '200px'
|
|
46
|
+
}
|
|
47
|
+
}, _TruncateText || (_TruncateText = /*#__PURE__*/React.createElement(TruncateText, null, "Hello world!", ' ', /*#__PURE__*/React.createElement("strong", null, /*#__PURE__*/React.createElement("span", null, "This is a")), ' ', "long string that should truncate"))));
|
|
48
|
+
const expectedErrorMessage = 'Some children are too deep in the node tree and will not render.';
|
|
49
|
+
await waitFor(() => {
|
|
50
|
+
expect(consoleErrorMock).toHaveBeenCalledWith(expect.stringContaining(expectedErrorMessage), expect.any(String));
|
|
51
|
+
});
|
|
52
|
+
});
|
|
53
|
+
it('should handle the empty string as a child', async () => {
|
|
54
|
+
let error = false;
|
|
55
|
+
try {
|
|
56
|
+
const _render = render(_TruncateText2 || (_TruncateText2 = /*#__PURE__*/React.createElement(TruncateText, null, ''))),
|
|
57
|
+
rerender = _render.rerender;
|
|
58
|
+
rerender(_TruncateText3 || (_TruncateText3 = /*#__PURE__*/React.createElement(TruncateText, null, 'hello world')));
|
|
59
|
+
rerender(_TruncateText4 || (_TruncateText4 = /*#__PURE__*/React.createElement(TruncateText, null, '')));
|
|
60
|
+
} catch (_e) {
|
|
61
|
+
error = true;
|
|
62
|
+
}
|
|
63
|
+
expect(error).toBe(false);
|
|
64
|
+
});
|
|
65
|
+
it('should meet a11y standards', async () => {
|
|
66
|
+
const _render2 = render(/*#__PURE__*/React.createElement("div", {
|
|
67
|
+
style: {
|
|
68
|
+
width: '200px'
|
|
69
|
+
}
|
|
70
|
+
}, _TruncateText5 || (_TruncateText5 = /*#__PURE__*/React.createElement(TruncateText, null, defaultText)))),
|
|
71
|
+
container = _render2.container;
|
|
72
|
+
const axeCheck = await runAxeCheck(container);
|
|
73
|
+
expect(axeCheck).toBe(true);
|
|
74
|
+
});
|
|
75
|
+
});
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* The MIT License (MIT)
|
|
3
|
+
*
|
|
4
|
+
* Copyright (c) 2015 - present Instructure, Inc.
|
|
5
|
+
*
|
|
6
|
+
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
7
|
+
* of this software and associated documentation files (the "Software"), to deal
|
|
8
|
+
* in the Software without restriction, including without limitation the rights
|
|
9
|
+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
10
|
+
* copies of the Software, and to permit persons to whom the Software is
|
|
11
|
+
* furnished to do so, subject to the following conditions:
|
|
12
|
+
*
|
|
13
|
+
* The above copyright notice and this permission notice shall be included in all
|
|
14
|
+
* copies or substantial portions of the Software.
|
|
15
|
+
*
|
|
16
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
17
|
+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
18
|
+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
19
|
+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
20
|
+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
21
|
+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
22
|
+
* SOFTWARE.
|
|
23
|
+
*/
|
|
24
|
+
import { expect } from 'chai';
|
|
25
|
+
import '@testing-library/jest-dom';
|
|
26
|
+
import cleanData from '../cleanData';
|
|
27
|
+
describe('cleanData', () => {
|
|
28
|
+
it('should remove spaces from the end of character data', async () => {
|
|
29
|
+
const data = [['T', 'e', 's', 't', ' ', '...']];
|
|
30
|
+
const options = {
|
|
31
|
+
truncate: 'character',
|
|
32
|
+
ellipsis: '...',
|
|
33
|
+
ignore: [' ']
|
|
34
|
+
};
|
|
35
|
+
const newData = cleanData(data, options);
|
|
36
|
+
expect(newData[0].join('')).to.equal('Test...');
|
|
37
|
+
});
|
|
38
|
+
it('should remove spaces from the end of word data', async () => {
|
|
39
|
+
const data = [['Test ', '...']];
|
|
40
|
+
const options = {
|
|
41
|
+
truncate: 'word',
|
|
42
|
+
ellipsis: '...',
|
|
43
|
+
ignore: [' ']
|
|
44
|
+
};
|
|
45
|
+
const newData = cleanData(data, options);
|
|
46
|
+
expect(newData[0].join('')).to.equal('Test...');
|
|
47
|
+
});
|
|
48
|
+
it('should remove spaces from the middle of character data', async () => {
|
|
49
|
+
const data = [['H', 'e', 'l', 'l', 'o', ' ', '...', ' ', 'w', 'o', 'r', 'l', 'd']];
|
|
50
|
+
const options = {
|
|
51
|
+
truncate: 'character',
|
|
52
|
+
ellipsis: '...',
|
|
53
|
+
ignore: [' ']
|
|
54
|
+
};
|
|
55
|
+
const newData = cleanData(data, options);
|
|
56
|
+
expect(newData[0].join('')).to.equal('Hello...world');
|
|
57
|
+
});
|
|
58
|
+
it('should remove spaces from the middle of word data', async () => {
|
|
59
|
+
const data = [['Hello ', '...', 'world']];
|
|
60
|
+
const options = {
|
|
61
|
+
truncate: 'word',
|
|
62
|
+
ellipsis: '...',
|
|
63
|
+
ignore: [' ']
|
|
64
|
+
};
|
|
65
|
+
const newData = cleanData(data, options);
|
|
66
|
+
expect(newData[0].join('')).to.equal('Hello...world');
|
|
67
|
+
});
|
|
68
|
+
it('should do a thorough cleaning', async () => {
|
|
69
|
+
const data = [['T', 'e', 's', 't', '.', ' ', '...']];
|
|
70
|
+
const options = {
|
|
71
|
+
truncate: 'character',
|
|
72
|
+
ellipsis: '...',
|
|
73
|
+
ignore: [' ', '.']
|
|
74
|
+
};
|
|
75
|
+
const newData = cleanData(data, options, true);
|
|
76
|
+
expect(newData[0].join('')).to.equal('Test...');
|
|
77
|
+
});
|
|
78
|
+
it('should remove spaces from the end of complex character data', async () => {
|
|
79
|
+
let data = [['H', 'e', 'l', 'l', 'o', ' '], ['...']];
|
|
80
|
+
const options = {
|
|
81
|
+
truncate: 'character',
|
|
82
|
+
ellipsis: '...',
|
|
83
|
+
ignore: [' ']
|
|
84
|
+
};
|
|
85
|
+
let newData = cleanData(data, options);
|
|
86
|
+
const text = newData[0].join('') + newData[1].join('');
|
|
87
|
+
data = [['H', 'e', 'l', 'l', 'o', ' '], ['w', 'o', 'r', 'l', 'd', ' ', '...']];
|
|
88
|
+
newData = cleanData(data, options);
|
|
89
|
+
const text2 = newData[0].join('') + newData[1].join('');
|
|
90
|
+
expect(text).to.equal('Hello...');
|
|
91
|
+
expect(text2).to.equal('Hello world...');
|
|
92
|
+
});
|
|
93
|
+
it('should remove spaces from the middle of complex word data', async () => {
|
|
94
|
+
let data = [['Hello ', '...'], ['world']];
|
|
95
|
+
const options = {
|
|
96
|
+
truncate: 'word',
|
|
97
|
+
ellipsis: '...',
|
|
98
|
+
ignore: [' ']
|
|
99
|
+
};
|
|
100
|
+
let newData = cleanData(data, options);
|
|
101
|
+
const text = newData[0].join('') + newData[1].join('');
|
|
102
|
+
data = [['Hello '], ['...', 'world']];
|
|
103
|
+
newData = cleanData(data, options);
|
|
104
|
+
const text2 = newData[0].join('') + newData[1].join('');
|
|
105
|
+
expect(text).to.equal('Hello...world');
|
|
106
|
+
expect(text2).to.equal('Hello...world');
|
|
107
|
+
});
|
|
108
|
+
});
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* The MIT License (MIT)
|
|
3
|
+
*
|
|
4
|
+
* Copyright (c) 2015 - present Instructure, Inc.
|
|
5
|
+
*
|
|
6
|
+
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
7
|
+
* of this software and associated documentation files (the "Software"), to deal
|
|
8
|
+
* in the Software without restriction, including without limitation the rights
|
|
9
|
+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
10
|
+
* copies of the Software, and to permit persons to whom the Software is
|
|
11
|
+
* furnished to do so, subject to the following conditions:
|
|
12
|
+
*
|
|
13
|
+
* The above copyright notice and this permission notice shall be included in all
|
|
14
|
+
* copies or substantial portions of the Software.
|
|
15
|
+
*
|
|
16
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
17
|
+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
18
|
+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
19
|
+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
20
|
+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
21
|
+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
22
|
+
* SOFTWARE.
|
|
23
|
+
*/
|
|
24
|
+
|
|
25
|
+
import { expect } from 'chai';
|
|
26
|
+
import '@testing-library/jest-dom';
|
|
27
|
+
import cleanString from '../cleanString';
|
|
28
|
+
describe('cleanSring', () => {
|
|
29
|
+
it('should remove spaces from start and end of string', async () => {
|
|
30
|
+
const string = ' Hello world ';
|
|
31
|
+
const newString = cleanString(string, [' ']);
|
|
32
|
+
expect(newString).to.equal('Hello world');
|
|
33
|
+
});
|
|
34
|
+
it('should remove spaces from only the end of string', async () => {
|
|
35
|
+
const string = ' Hello world ';
|
|
36
|
+
const newString = cleanString(string, [' '], false);
|
|
37
|
+
expect(newString).to.equal(' Hello world');
|
|
38
|
+
});
|
|
39
|
+
it('should remove spaces and commas', async () => {
|
|
40
|
+
const string = ' Hello world,';
|
|
41
|
+
const newString = cleanString(string, [' ', ',']);
|
|
42
|
+
expect(newString).to.equal('Hello world');
|
|
43
|
+
});
|
|
44
|
+
it('should do a thorough cleaning', async () => {
|
|
45
|
+
const string = 'Hello world. ';
|
|
46
|
+
const newString = cleanString(string, [' ', '.'], false, true, true);
|
|
47
|
+
expect(newString).to.equal('Hello world');
|
|
48
|
+
});
|
|
49
|
+
});
|
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+
var _div, _div2, _div3, _div4;
|
|
2
|
+
/*
|
|
3
|
+
* The MIT License (MIT)
|
|
4
|
+
*
|
|
5
|
+
* Copyright (c) 2015 - present Instructure, Inc.
|
|
6
|
+
*
|
|
7
|
+
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
8
|
+
* of this software and associated documentation files (the "Software"), to deal
|
|
9
|
+
* in the Software without restriction, including without limitation the rights
|
|
10
|
+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
11
|
+
* copies of the Software, and to permit persons to whom the Software is
|
|
12
|
+
* furnished to do so, subject to the following conditions:
|
|
13
|
+
*
|
|
14
|
+
* The above copyright notice and this permission notice shall be included in all
|
|
15
|
+
* copies or substantial portions of the Software.
|
|
16
|
+
*
|
|
17
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
18
|
+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
19
|
+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
20
|
+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
21
|
+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
22
|
+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
23
|
+
* SOFTWARE.
|
|
24
|
+
*/
|
|
25
|
+
|
|
26
|
+
import React from 'react';
|
|
27
|
+
import { render, screen } from '@testing-library/react';
|
|
28
|
+
import { vi, expect } from 'vitest';
|
|
29
|
+
import '@testing-library/jest-dom';
|
|
30
|
+
import measureText from '../measureText';
|
|
31
|
+
const baseStyle = {
|
|
32
|
+
fontSize: '16px',
|
|
33
|
+
fontFamily: 'Arial',
|
|
34
|
+
fontWeight: 'normal',
|
|
35
|
+
fontStyle: 'normal',
|
|
36
|
+
letterSpacing: 'normal'
|
|
37
|
+
};
|
|
38
|
+
const getNodes = root => Array.from(root.childNodes).filter(node => node.nodeType === Node.ELEMENT_NODE || node.nodeType === Node.TEXT_NODE);
|
|
39
|
+
describe('measureText', () => {
|
|
40
|
+
beforeEach(() => {
|
|
41
|
+
vi.spyOn(HTMLCanvasElement.prototype, 'getContext').mockImplementation(() => {
|
|
42
|
+
return {
|
|
43
|
+
font: '',
|
|
44
|
+
measureText: vi.fn(text => {
|
|
45
|
+
return {
|
|
46
|
+
width: text.length * 10
|
|
47
|
+
};
|
|
48
|
+
})
|
|
49
|
+
};
|
|
50
|
+
});
|
|
51
|
+
});
|
|
52
|
+
afterEach(() => {
|
|
53
|
+
vi.restoreAllMocks();
|
|
54
|
+
});
|
|
55
|
+
it('should calculate the width of a single text node correctly', () => {
|
|
56
|
+
const node = document.createElement('div');
|
|
57
|
+
Object.assign(node.style, baseStyle);
|
|
58
|
+
node.textContent = 'Hello';
|
|
59
|
+
const width = measureText([node]);
|
|
60
|
+
expect(width).toBe(50); // 5 characters * 10 (mocked width per character)
|
|
61
|
+
});
|
|
62
|
+
it('should return zero for empty nodes', () => {
|
|
63
|
+
const node = document.createElement('div');
|
|
64
|
+
Object.assign(node.style, baseStyle);
|
|
65
|
+
node.textContent = '';
|
|
66
|
+
const width = measureText([node]);
|
|
67
|
+
expect(width).toBe(0);
|
|
68
|
+
});
|
|
69
|
+
it('should calculate text width correctly', () => {
|
|
70
|
+
render(_div || (_div = /*#__PURE__*/React.createElement("div", {
|
|
71
|
+
"data-testid": "stage",
|
|
72
|
+
style: baseStyle
|
|
73
|
+
}, "Lorem ipsum ", /*#__PURE__*/React.createElement("span", {
|
|
74
|
+
style: baseStyle
|
|
75
|
+
}, "DOLOR SIT AMET."))));
|
|
76
|
+
const stage = screen.getByTestId('stage');
|
|
77
|
+
const nodes = getNodes(stage);
|
|
78
|
+
const width = measureText(nodes, stage);
|
|
79
|
+
expect(width).toBe(270);
|
|
80
|
+
});
|
|
81
|
+
it('should account for different nodes', async () => {
|
|
82
|
+
const _render = render(_div2 || (_div2 = /*#__PURE__*/React.createElement("div", {
|
|
83
|
+
"data-testid": "stage",
|
|
84
|
+
style: baseStyle
|
|
85
|
+
}, "Lorem ipsum ", /*#__PURE__*/React.createElement("span", {
|
|
86
|
+
style: baseStyle
|
|
87
|
+
}, "DOLOR SIT AMET.")))),
|
|
88
|
+
rerender = _render.rerender;
|
|
89
|
+
const stage = screen.getByTestId('stage');
|
|
90
|
+
const nodes = getNodes(stage);
|
|
91
|
+
const width = measureText(nodes, stage);
|
|
92
|
+
|
|
93
|
+
// Set child
|
|
94
|
+
rerender(_div3 || (_div3 = /*#__PURE__*/React.createElement("div", {
|
|
95
|
+
"data-testid": "stage",
|
|
96
|
+
style: baseStyle
|
|
97
|
+
}, "Lorem ipsum DOLOR SIT AMET.")));
|
|
98
|
+
const stage2 = screen.getByTestId('stage');
|
|
99
|
+
const nodes2 = getNodes(stage2);
|
|
100
|
+
const width2 = measureText(nodes2, stage2);
|
|
101
|
+
expect(width).toEqual(width2);
|
|
102
|
+
});
|
|
103
|
+
it('should call measureText on a properly configured canvas 2d context', () => {
|
|
104
|
+
const mockedStyle = {
|
|
105
|
+
fontSize: '20px',
|
|
106
|
+
fontWeight: 'bold',
|
|
107
|
+
fontStyle: 'italic',
|
|
108
|
+
fontFamily: 'Arial'
|
|
109
|
+
};
|
|
110
|
+
render(/*#__PURE__*/React.createElement("div", {
|
|
111
|
+
"data-testid": "stage",
|
|
112
|
+
style: mockedStyle
|
|
113
|
+
}, "Lorem ipsum"));
|
|
114
|
+
const stage = screen.getByTestId('stage');
|
|
115
|
+
const nodes = getNodes(stage);
|
|
116
|
+
measureText(nodes, stage);
|
|
117
|
+
|
|
118
|
+
// The width calculation depends on the modified canvas context
|
|
119
|
+
const getContextSpy = HTMLCanvasElement.prototype.getContext;
|
|
120
|
+
const context = getContextSpy.mock.results[0].value;
|
|
121
|
+
expect(context.measureText).toHaveBeenCalledTimes(1);
|
|
122
|
+
expect(context.font).toBe('bold italic 20px Arial');
|
|
123
|
+
});
|
|
124
|
+
it('should account for letter spacing styles', async () => {
|
|
125
|
+
const _render2 = render(_div4 || (_div4 = /*#__PURE__*/React.createElement("div", {
|
|
126
|
+
"data-testid": "stage",
|
|
127
|
+
style: baseStyle
|
|
128
|
+
}, "Lorem ipsum"))),
|
|
129
|
+
rerender = _render2.rerender;
|
|
130
|
+
const stage = screen.getByTestId('stage');
|
|
131
|
+
const nodes = getNodes(stage);
|
|
132
|
+
const width = measureText(nodes, stage);
|
|
133
|
+
expect(width).toBe(110); // default mocked width (text.length * 10)
|
|
134
|
+
|
|
135
|
+
// Set letterSpacing
|
|
136
|
+
rerender(/*#__PURE__*/React.createElement("div", {
|
|
137
|
+
"data-testid": "stage2",
|
|
138
|
+
style: {
|
|
139
|
+
...baseStyle,
|
|
140
|
+
letterSpacing: '5px'
|
|
141
|
+
}
|
|
142
|
+
}, "Lorem ipsum"));
|
|
143
|
+
const stage2 = screen.getByTestId('stage2');
|
|
144
|
+
const nodes2 = getNodes(stage2);
|
|
145
|
+
const width2 = measureText(nodes2, stage2);
|
|
146
|
+
expect(width2).toBe(165); // default mocked width + letterOffset (text.length * letterSpacing)
|
|
147
|
+
});
|
|
148
|
+
});
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
|
|
4
|
+
var _react = _interopRequireDefault(require("react"));
|
|
5
|
+
var _react2 = require("@testing-library/react");
|
|
6
|
+
var _vitest = require("vitest");
|
|
7
|
+
require("@testing-library/jest-dom");
|
|
8
|
+
var _runAxeCheck = require("@instructure/ui-axe-check/lib/runAxeCheck.js");
|
|
9
|
+
var _index = require("../index");
|
|
10
|
+
var _TruncateText, _TruncateText2, _TruncateText3, _TruncateText4, _TruncateText5;
|
|
11
|
+
/*
|
|
12
|
+
* The MIT License (MIT)
|
|
13
|
+
*
|
|
14
|
+
* Copyright (c) 2015 - present Instructure, Inc.
|
|
15
|
+
*
|
|
16
|
+
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
17
|
+
* of this software and associated documentation files (the "Software"), to deal
|
|
18
|
+
* in the Software without restriction, including without limitation the rights
|
|
19
|
+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
20
|
+
* copies of the Software, and to permit persons to whom the Software is
|
|
21
|
+
* furnished to do so, subject to the following conditions:
|
|
22
|
+
*
|
|
23
|
+
* The above copyright notice and this permission notice shall be included in all
|
|
24
|
+
* copies or substantial portions of the Software.
|
|
25
|
+
*
|
|
26
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
27
|
+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
28
|
+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
29
|
+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
30
|
+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
31
|
+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
32
|
+
* SOFTWARE.
|
|
33
|
+
*/
|
|
34
|
+
const defaultText = 'Hello world! This is a long string that should truncate';
|
|
35
|
+
describe('<TruncateText />', () => {
|
|
36
|
+
let consoleErrorMock;
|
|
37
|
+
beforeEach(() => {
|
|
38
|
+
// Mocking console to prevent test output pollution and expect for messages
|
|
39
|
+
consoleErrorMock = _vitest.vi.spyOn(console, 'error').mockImplementation(() => {});
|
|
40
|
+
});
|
|
41
|
+
afterEach(() => {
|
|
42
|
+
consoleErrorMock.mockRestore();
|
|
43
|
+
});
|
|
44
|
+
it('should warn if children prop receives too deep of a node tree', async () => {
|
|
45
|
+
(0, _react2.render)(/*#__PURE__*/_react.default.createElement("div", {
|
|
46
|
+
style: {
|
|
47
|
+
width: '200px'
|
|
48
|
+
}
|
|
49
|
+
}, _TruncateText || (_TruncateText = /*#__PURE__*/_react.default.createElement(_index.TruncateText, null, "Hello world!", ' ', /*#__PURE__*/_react.default.createElement("strong", null, /*#__PURE__*/_react.default.createElement("span", null, "This is a")), ' ', "long string that should truncate"))));
|
|
50
|
+
const expectedErrorMessage = 'Some children are too deep in the node tree and will not render.';
|
|
51
|
+
await (0, _react2.waitFor)(() => {
|
|
52
|
+
(0, _vitest.expect)(consoleErrorMock).toHaveBeenCalledWith(_vitest.expect.stringContaining(expectedErrorMessage), _vitest.expect.any(String));
|
|
53
|
+
});
|
|
54
|
+
});
|
|
55
|
+
it('should handle the empty string as a child', async () => {
|
|
56
|
+
let error = false;
|
|
57
|
+
try {
|
|
58
|
+
const _render = (0, _react2.render)(_TruncateText2 || (_TruncateText2 = /*#__PURE__*/_react.default.createElement(_index.TruncateText, null, ''))),
|
|
59
|
+
rerender = _render.rerender;
|
|
60
|
+
rerender(_TruncateText3 || (_TruncateText3 = /*#__PURE__*/_react.default.createElement(_index.TruncateText, null, 'hello world')));
|
|
61
|
+
rerender(_TruncateText4 || (_TruncateText4 = /*#__PURE__*/_react.default.createElement(_index.TruncateText, null, '')));
|
|
62
|
+
} catch (_e) {
|
|
63
|
+
error = true;
|
|
64
|
+
}
|
|
65
|
+
(0, _vitest.expect)(error).toBe(false);
|
|
66
|
+
});
|
|
67
|
+
it('should meet a11y standards', async () => {
|
|
68
|
+
const _render2 = (0, _react2.render)(/*#__PURE__*/_react.default.createElement("div", {
|
|
69
|
+
style: {
|
|
70
|
+
width: '200px'
|
|
71
|
+
}
|
|
72
|
+
}, _TruncateText5 || (_TruncateText5 = /*#__PURE__*/_react.default.createElement(_index.TruncateText, null, defaultText)))),
|
|
73
|
+
container = _render2.container;
|
|
74
|
+
const axeCheck = await (0, _runAxeCheck.runAxeCheck)(container);
|
|
75
|
+
(0, _vitest.expect)(axeCheck).toBe(true);
|
|
76
|
+
});
|
|
77
|
+
});
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
|
|
4
|
+
var _chai = require("chai");
|
|
5
|
+
require("@testing-library/jest-dom");
|
|
6
|
+
var _cleanData = _interopRequireDefault(require("../cleanData"));
|
|
7
|
+
/*
|
|
8
|
+
* The MIT License (MIT)
|
|
9
|
+
*
|
|
10
|
+
* Copyright (c) 2015 - present Instructure, Inc.
|
|
11
|
+
*
|
|
12
|
+
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
13
|
+
* of this software and associated documentation files (the "Software"), to deal
|
|
14
|
+
* in the Software without restriction, including without limitation the rights
|
|
15
|
+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
16
|
+
* copies of the Software, and to permit persons to whom the Software is
|
|
17
|
+
* furnished to do so, subject to the following conditions:
|
|
18
|
+
*
|
|
19
|
+
* The above copyright notice and this permission notice shall be included in all
|
|
20
|
+
* copies or substantial portions of the Software.
|
|
21
|
+
*
|
|
22
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
23
|
+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
24
|
+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
25
|
+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
26
|
+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
27
|
+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
28
|
+
* SOFTWARE.
|
|
29
|
+
*/
|
|
30
|
+
|
|
31
|
+
describe('cleanData', () => {
|
|
32
|
+
it('should remove spaces from the end of character data', async () => {
|
|
33
|
+
const data = [['T', 'e', 's', 't', ' ', '...']];
|
|
34
|
+
const options = {
|
|
35
|
+
truncate: 'character',
|
|
36
|
+
ellipsis: '...',
|
|
37
|
+
ignore: [' ']
|
|
38
|
+
};
|
|
39
|
+
const newData = (0, _cleanData.default)(data, options);
|
|
40
|
+
(0, _chai.expect)(newData[0].join('')).to.equal('Test...');
|
|
41
|
+
});
|
|
42
|
+
it('should remove spaces from the end of word data', async () => {
|
|
43
|
+
const data = [['Test ', '...']];
|
|
44
|
+
const options = {
|
|
45
|
+
truncate: 'word',
|
|
46
|
+
ellipsis: '...',
|
|
47
|
+
ignore: [' ']
|
|
48
|
+
};
|
|
49
|
+
const newData = (0, _cleanData.default)(data, options);
|
|
50
|
+
(0, _chai.expect)(newData[0].join('')).to.equal('Test...');
|
|
51
|
+
});
|
|
52
|
+
it('should remove spaces from the middle of character data', async () => {
|
|
53
|
+
const data = [['H', 'e', 'l', 'l', 'o', ' ', '...', ' ', 'w', 'o', 'r', 'l', 'd']];
|
|
54
|
+
const options = {
|
|
55
|
+
truncate: 'character',
|
|
56
|
+
ellipsis: '...',
|
|
57
|
+
ignore: [' ']
|
|
58
|
+
};
|
|
59
|
+
const newData = (0, _cleanData.default)(data, options);
|
|
60
|
+
(0, _chai.expect)(newData[0].join('')).to.equal('Hello...world');
|
|
61
|
+
});
|
|
62
|
+
it('should remove spaces from the middle of word data', async () => {
|
|
63
|
+
const data = [['Hello ', '...', 'world']];
|
|
64
|
+
const options = {
|
|
65
|
+
truncate: 'word',
|
|
66
|
+
ellipsis: '...',
|
|
67
|
+
ignore: [' ']
|
|
68
|
+
};
|
|
69
|
+
const newData = (0, _cleanData.default)(data, options);
|
|
70
|
+
(0, _chai.expect)(newData[0].join('')).to.equal('Hello...world');
|
|
71
|
+
});
|
|
72
|
+
it('should do a thorough cleaning', async () => {
|
|
73
|
+
const data = [['T', 'e', 's', 't', '.', ' ', '...']];
|
|
74
|
+
const options = {
|
|
75
|
+
truncate: 'character',
|
|
76
|
+
ellipsis: '...',
|
|
77
|
+
ignore: [' ', '.']
|
|
78
|
+
};
|
|
79
|
+
const newData = (0, _cleanData.default)(data, options, true);
|
|
80
|
+
(0, _chai.expect)(newData[0].join('')).to.equal('Test...');
|
|
81
|
+
});
|
|
82
|
+
it('should remove spaces from the end of complex character data', async () => {
|
|
83
|
+
let data = [['H', 'e', 'l', 'l', 'o', ' '], ['...']];
|
|
84
|
+
const options = {
|
|
85
|
+
truncate: 'character',
|
|
86
|
+
ellipsis: '...',
|
|
87
|
+
ignore: [' ']
|
|
88
|
+
};
|
|
89
|
+
let newData = (0, _cleanData.default)(data, options);
|
|
90
|
+
const text = newData[0].join('') + newData[1].join('');
|
|
91
|
+
data = [['H', 'e', 'l', 'l', 'o', ' '], ['w', 'o', 'r', 'l', 'd', ' ', '...']];
|
|
92
|
+
newData = (0, _cleanData.default)(data, options);
|
|
93
|
+
const text2 = newData[0].join('') + newData[1].join('');
|
|
94
|
+
(0, _chai.expect)(text).to.equal('Hello...');
|
|
95
|
+
(0, _chai.expect)(text2).to.equal('Hello world...');
|
|
96
|
+
});
|
|
97
|
+
it('should remove spaces from the middle of complex word data', async () => {
|
|
98
|
+
let data = [['Hello ', '...'], ['world']];
|
|
99
|
+
const options = {
|
|
100
|
+
truncate: 'word',
|
|
101
|
+
ellipsis: '...',
|
|
102
|
+
ignore: [' ']
|
|
103
|
+
};
|
|
104
|
+
let newData = (0, _cleanData.default)(data, options);
|
|
105
|
+
const text = newData[0].join('') + newData[1].join('');
|
|
106
|
+
data = [['Hello '], ['...', 'world']];
|
|
107
|
+
newData = (0, _cleanData.default)(data, options);
|
|
108
|
+
const text2 = newData[0].join('') + newData[1].join('');
|
|
109
|
+
(0, _chai.expect)(text).to.equal('Hello...world');
|
|
110
|
+
(0, _chai.expect)(text2).to.equal('Hello...world');
|
|
111
|
+
});
|
|
112
|
+
});
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
|
|
4
|
+
var _chai = require("chai");
|
|
5
|
+
require("@testing-library/jest-dom");
|
|
6
|
+
var _cleanString = _interopRequireDefault(require("../cleanString"));
|
|
7
|
+
/*
|
|
8
|
+
* The MIT License (MIT)
|
|
9
|
+
*
|
|
10
|
+
* Copyright (c) 2015 - present Instructure, Inc.
|
|
11
|
+
*
|
|
12
|
+
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
13
|
+
* of this software and associated documentation files (the "Software"), to deal
|
|
14
|
+
* in the Software without restriction, including without limitation the rights
|
|
15
|
+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
16
|
+
* copies of the Software, and to permit persons to whom the Software is
|
|
17
|
+
* furnished to do so, subject to the following conditions:
|
|
18
|
+
*
|
|
19
|
+
* The above copyright notice and this permission notice shall be included in all
|
|
20
|
+
* copies or substantial portions of the Software.
|
|
21
|
+
*
|
|
22
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
23
|
+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
24
|
+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
25
|
+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
26
|
+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
27
|
+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
28
|
+
* SOFTWARE.
|
|
29
|
+
*/
|
|
30
|
+
|
|
31
|
+
describe('cleanSring', () => {
|
|
32
|
+
it('should remove spaces from start and end of string', async () => {
|
|
33
|
+
const string = ' Hello world ';
|
|
34
|
+
const newString = (0, _cleanString.default)(string, [' ']);
|
|
35
|
+
(0, _chai.expect)(newString).to.equal('Hello world');
|
|
36
|
+
});
|
|
37
|
+
it('should remove spaces from only the end of string', async () => {
|
|
38
|
+
const string = ' Hello world ';
|
|
39
|
+
const newString = (0, _cleanString.default)(string, [' '], false);
|
|
40
|
+
(0, _chai.expect)(newString).to.equal(' Hello world');
|
|
41
|
+
});
|
|
42
|
+
it('should remove spaces and commas', async () => {
|
|
43
|
+
const string = ' Hello world,';
|
|
44
|
+
const newString = (0, _cleanString.default)(string, [' ', ',']);
|
|
45
|
+
(0, _chai.expect)(newString).to.equal('Hello world');
|
|
46
|
+
});
|
|
47
|
+
it('should do a thorough cleaning', async () => {
|
|
48
|
+
const string = 'Hello world. ';
|
|
49
|
+
const newString = (0, _cleanString.default)(string, [' ', '.'], false, true, true);
|
|
50
|
+
(0, _chai.expect)(newString).to.equal('Hello world');
|
|
51
|
+
});
|
|
52
|
+
});
|