Skip to content

Commit 64d6d6a

Browse files
refactor(Device): deprecate Device class and move all methods to smaller util functions (#697)
1 parent cce75e2 commit 64d6d6a

30 files changed

+1003
-822
lines changed

.eslintignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,4 @@ shared
1010
rollup.config.js
1111
index.cjs.js
1212
index.esm.js
13-
packages/base/third-party/
1413
packages/cra-template

.reuse/dep5

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,7 @@ License: Apache-2.0
1111
Files: /packages/charts/src/components/PieChart/PieChart.tsx
1212
Copyright: 2016-2020 Recharts Group
1313
License: MIT
14+
15+
Files: packages/base/src/hooks/useIsomorphicLayoutEffect.ts
16+
Copyright: 2019 Tanner Linsley
17+
License: MIT

.storybook/main.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ module.exports = {
8181
'@ui5/webcomponents-react/dist': path.join(PATHS.root, 'packages', 'main', 'dist'),
8282
'@ui5/webcomponents-react': path.join(PATHS.root, 'packages', 'main', 'src'),
8383
'@ui5/webcomponents-react-charts': path.join(PATHS.root, 'packages', 'charts', 'src'),
84-
'@ui5/webcomponents-react-base/third-party': path.join(PATHS.root, 'packages', 'base', 'third-party'),
84+
'@ui5/webcomponents-react-base/types': path.join(PATHS.root, 'packages', 'base', 'types'),
8585
'@ui5/webcomponents-react-base': path.join(PATHS.root, 'packages', 'base', 'src')
8686
};
8787

LICENSES/MIT.txt

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
MIT License Copyright (c) <year> <copyright holders>
2+
3+
Permission is hereby granted, free of charge, to any person obtaining a copy
4+
of this software and associated documentation files (the "Software"), to deal
5+
in the Software without restriction, including without limitation the rights
6+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7+
copies of the Software, and to permit persons to whom the Software is furnished
8+
to do so, subject to the following conditions:
9+
10+
The above copyright notice and this permission notice (including the next
11+
paragraph) shall be included in all copies or substantial portions of the
12+
Software.
13+
14+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
16+
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS
17+
OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
18+
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
19+
OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

config/jest.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ module.exports = {
2828
'^@shared/(.*)$': '<rootDir>/shared/$1',
2929
'^@ui5/webcomponents-react/dist/(.*)$': '<rootDir>/packages/main/dist/$1',
3030
'^@ui5/webcomponents-react/(.*)$': '<rootDir>/packages/main/src/$1',
31-
'^@ui5/webcomponents-react-base/third-party/(.*)$': '<rootDir>/packages/base/third-party/$1',
31+
'^@ui5/webcomponents-react-base/types(.*)$': '<rootDir>/packages/base/types/$1',
3232
'^@ui5/webcomponents-react-base/(.*)$': '<rootDir>/packages/base/src/$1',
3333
'^@ui5/webcomponents-react-charts/(.*)$': '<rootDir>/packages/charts/src/$1',
3434
'\\.(css|less)$': 'identity-obj-proxy'

config/jestsetup.ts

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -38,14 +38,19 @@ expect.addSnapshotSerializer(jssSerializer);
3838
expect.addSnapshotSerializer(contentLoaderSerializer);
3939

4040
export const setupMatchMedia = () => {
41-
// @ts-ignore
42-
window.matchMedia = () => {
43-
return {
44-
matches: true,
45-
addListener() {},
46-
removeListener() {}
47-
};
48-
};
41+
Object.defineProperty(window, 'matchMedia', {
42+
writable: true,
43+
value: jest.fn().mockImplementation((query) => ({
44+
matches: false,
45+
media: query,
46+
onchange: null,
47+
addListener: jest.fn(), // deprecated
48+
removeListener: jest.fn(), // deprecated
49+
addEventListener: jest.fn(),
50+
removeEventListener: jest.fn(),
51+
dispatchEvent: jest.fn()
52+
}))
53+
});
4954
};
5055

5156
export const setupResizeObserver = () => {

packages/base/package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,12 @@
1414
"author": "SAP SE (https://www.sap.com)",
1515
"license": "Apache-2.0",
1616
"sideEffects": [
17-
"**/third-party/*",
1817
"./polyfill/*",
1918
"core-js/**/*",
2019
"intersection-observer"
2120
],
2221
"scripts": {
23-
"clean": "rimraf cjs Device hooks lib polyfill styling types utils index.esm.js index.d.ts",
22+
"clean": "rimraf cjs Device hooks lib polyfill styling utils index.esm.js index.d.ts",
2423
"build:rollup": "rollup -c rollup.config.js",
2524
"build:polyfills": "tsc ./src/polyfill/*.ts --outDir ./polyfill --skipLibCheck",
2625
"build": "npm-run-all -s build:rollup build:polyfills",

packages/base/src/Device/Media.test.ts

Lines changed: 10 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,21 @@
1-
import { setupMatchMedia } from '../../../../config/jestsetup';
21
import { EventRegistry } from './EventRegistry';
3-
import { Media, RANGESETS } from './Media';
4-
5-
const defaultSupportInstance = {
6-
matchmedialistener: false,
7-
matchmedia: false
8-
};
2+
import { getCurrentRange, MediaLegacy, RANGESETS } from './Media';
93

104
describe('Device - Media', () => {
115
test('invoke constructor', () => {
12-
const media = new Media(defaultSupportInstance);
13-
// @ts-ignore
14-
expect(media.mediaTimeout).toBe(100);
6+
const media = new MediaLegacy();
157
expect(media.hasRangeSet(RANGESETS.SAP_STANDARD)).toBe(true);
168
expect(media.hasRangeSet(RANGESETS.SAP_STANDARD_EXTENDED)).toBe(true);
17-
});
18-
19-
test('invoke constructor 2', () => {
20-
const media = new Media({ matchmedia: true, matchmedialistener: true });
21-
// @ts-ignore
22-
expect(media.mediaTimeout).toBe(0);
9+
media.initRangeSet(RANGESETS.SAP_STANDARD_EXTENDED);
2310
expect(media.hasRangeSet(RANGESETS.SAP_STANDARD)).toBe(true);
24-
expect(media.hasRangeSet(RANGESETS.SAP_STANDARD_EXTENDED)).toBe(true);
2511
});
2612

27-
test('matchLegacy', () => {
28-
const media = new Media(defaultSupportInstance);
29-
// @ts-ignore
30-
window.matchMedia = () => {
31-
return false;
32-
};
33-
expect(media.matches(19, 20, 'rem')).toBe(false);
34-
});
35-
36-
test('match', () => {
37-
const media = new Media({ matchmedia: true, matchmedialistener: true });
38-
// @ts-ignore
39-
window.matchMedia = () => {
40-
return { matches: true };
41-
};
42-
expect(media.matches(19, 20, 'rem')).toBe(true);
13+
test('getCurrentRangeSet with undefined Range Set', () => {
14+
expect(getCurrentRange('Not_Defined', 100)).toBeNull();
4315
});
4416

4517
test('Attach, fire and Detach Event', () => {
46-
const media = new Media(defaultSupportInstance);
18+
const media = new MediaLegacy();
4719
const callback = jest.fn();
4820
// @ts-ignore
4921
media.attachHandler(callback, this);
@@ -56,27 +28,26 @@ describe('Device - Media', () => {
5628
});
5729

5830
test('Init Custom Range Set and remove it again', () => {
59-
const media = new Media(defaultSupportInstance);
31+
const media = new MediaLegacy();
6032
media.initRangeSet('MyRangeSet', [200, 400], 'px', ['Small', 'Medium', 'Large']);
6133
expect(media.getCurrentRange('MyRangeSet', 300).name).toBe('Medium');
6234
media.removeRangeSet('MyRangeSet');
6335
});
6436

6537
test('Init Custom Range Set and remove it again w/ matchmedialistener', () => {
66-
setupMatchMedia();
67-
const media = new Media({ matchmedia: true, matchmedialistener: true });
38+
const media = new MediaLegacy();
6839
media.initRangeSet('MyRangeSet', [200, 400], 'px', ['Small', 'Medium', 'Large']);
6940
expect(media.getCurrentRange('MyRangeSet', 300).name).toBe('Medium');
7041
media.removeRangeSet('MyRangeSet');
7142
});
7243

7344
test('Remove unknown rangeset should not crash', () => {
74-
const media = new Media(defaultSupportInstance);
45+
const media = new MediaLegacy();
7546
expect(() => media.removeRangeSet('MyRangeSet')).not.toThrow();
7647
});
7748

7849
test("Don't remove build in rangesets", () => {
79-
const media = new Media(defaultSupportInstance);
50+
const media = new MediaLegacy();
8051
media.removeRangeSet(RANGESETS.SAP_STANDARD);
8152
expect(media.hasRangeSet(RANGESETS.SAP_STANDARD)).toBe(true);
8253
});

0 commit comments

Comments
 (0)