@instructure/ui-i18n 10.16.1 → 10.16.3
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 +16 -0
- package/es/ApplyLocale/__new-tests__/ApplyLocale.test.js +62 -0
- package/es/__new-tests__/DateTime.test.js +154 -0
- package/es/__new-tests__/I18nPropTypes.test.js +53 -0
- package/es/__new-tests__/Locale.test.js +52 -0
- package/es/__new-tests__/textDirectionContextConsumer.test.js +108 -0
- package/lib/ApplyLocale/__new-tests__/ApplyLocale.test.js +64 -0
- package/lib/__new-tests__/DateTime.test.js +157 -0
- package/lib/__new-tests__/I18nPropTypes.test.js +56 -0
- package/lib/__new-tests__/Locale.test.js +54 -0
- package/lib/__new-tests__/textDirectionContextConsumer.test.js +110 -0
- package/package.json +12 -10
- package/src/ApplyLocale/__new-tests__/ApplyLocale.test.tsx +63 -0
- package/src/__new-tests__/DateTime.test.tsx +224 -0
- package/src/__new-tests__/I18nPropTypes.test.tsx +119 -0
- package/src/__new-tests__/Locale.test.tsx +55 -0
- package/src/__new-tests__/textDirectionContextConsumer.test.tsx +125 -0
- package/tsconfig.build.json +1 -2
- package/tsconfig.build.tsbuildinfo +1 -1
- package/types/ApplyLocale/__new-tests__/ApplyLocale.test.d.ts +2 -0
- package/types/ApplyLocale/__new-tests__/ApplyLocale.test.d.ts.map +1 -0
- package/types/__new-tests__/DateTime.test.d.ts +2 -0
- package/types/__new-tests__/DateTime.test.d.ts.map +1 -0
- package/types/__new-tests__/I18nPropTypes.test.d.ts +2 -0
- package/types/__new-tests__/I18nPropTypes.test.d.ts.map +1 -0
- package/types/__new-tests__/Locale.test.d.ts +2 -0
- package/types/__new-tests__/Locale.test.d.ts.map +1 -0
- package/types/__new-tests__/textDirectionContextConsumer.test.d.ts +2 -0
- package/types/__new-tests__/textDirectionContextConsumer.test.d.ts.map +1 -0
package/CHANGELOG.md
CHANGED
|
@@ -3,6 +3,22 @@
|
|
|
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.16.3](https://github.com/instructure/instructure-ui/compare/v10.16.1...v10.16.3) (2025-04-30)
|
|
7
|
+
|
|
8
|
+
**Note:** Version bump only for package @instructure/ui-i18n
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
## [10.16.2](https://github.com/instructure/instructure-ui/compare/v10.16.1...v10.16.2) (2025-04-22)
|
|
15
|
+
|
|
16
|
+
**Note:** Version bump only for package @instructure/ui-i18n
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
|
|
6
22
|
## [10.16.1](https://github.com/instructure/instructure-ui/compare/v10.16.0...v10.16.1) (2025-04-22)
|
|
7
23
|
|
|
8
24
|
**Note:** Version bump only for package @instructure/ui-i18n
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
var _ApplyLocale, _ApplyLocale2;
|
|
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
|
+
import { Component } from 'react';
|
|
26
|
+
import { render } from '@testing-library/react';
|
|
27
|
+
import '@testing-library/jest-dom';
|
|
28
|
+
import { expect } from 'vitest';
|
|
29
|
+
import { ApplyLocale } from '../index';
|
|
30
|
+
import { ApplyLocaleContext } from '../ApplyLocaleContext';
|
|
31
|
+
import { jsx as _jsx, jsxs as _jsxs } from "@emotion/react/jsx-runtime";
|
|
32
|
+
class LocalizableComponent extends Component {
|
|
33
|
+
render() {
|
|
34
|
+
return _jsxs("div", {
|
|
35
|
+
children: [_jsx("span", {
|
|
36
|
+
children: this.context.locale
|
|
37
|
+
}), _jsx("span", {
|
|
38
|
+
children: this.context.timezone
|
|
39
|
+
})]
|
|
40
|
+
});
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
LocalizableComponent.displayName = "LocalizableComponent";
|
|
44
|
+
LocalizableComponent.contextType = ApplyLocaleContext;
|
|
45
|
+
describe('<ApplyLocale />', async () => {
|
|
46
|
+
it('applies locale context', async () => {
|
|
47
|
+
const _render = render(_ApplyLocale || (_ApplyLocale = _jsx(ApplyLocale, {
|
|
48
|
+
locale: "fr",
|
|
49
|
+
children: _jsx(LocalizableComponent, {})
|
|
50
|
+
}))),
|
|
51
|
+
container = _render.container;
|
|
52
|
+
expect(container).toHaveTextContent('fr');
|
|
53
|
+
});
|
|
54
|
+
it('applies timezone context', async () => {
|
|
55
|
+
const _render2 = render(_ApplyLocale2 || (_ApplyLocale2 = _jsx(ApplyLocale, {
|
|
56
|
+
timezone: "Europe/Paris",
|
|
57
|
+
children: _jsx(LocalizableComponent, {})
|
|
58
|
+
}))),
|
|
59
|
+
container = _render2.container;
|
|
60
|
+
expect(container).toHaveTextContent('Europe/Paris');
|
|
61
|
+
});
|
|
62
|
+
});
|
|
@@ -0,0 +1,154 @@
|
|
|
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 '@testing-library/jest-dom';
|
|
25
|
+
import { expect } from 'vitest';
|
|
26
|
+
import { DateTime } from '../DateTime';
|
|
27
|
+
const now = DateTime.now,
|
|
28
|
+
isValid = DateTime.isValid,
|
|
29
|
+
browserTimeZone = DateTime.browserTimeZone,
|
|
30
|
+
parse = DateTime.parse,
|
|
31
|
+
toLocaleString = DateTime.toLocaleString,
|
|
32
|
+
getFirstDayOfWeek = DateTime.getFirstDayOfWeek,
|
|
33
|
+
getLocalDayNamesOfTheWeek = DateTime.getLocalDayNamesOfTheWeek;
|
|
34
|
+
describe('DateTime', () => {
|
|
35
|
+
const timezone = 'America/Halifax'; // -3
|
|
36
|
+
const locale = 'en';
|
|
37
|
+
it('checks params', () => {
|
|
38
|
+
let whoops = false;
|
|
39
|
+
try {
|
|
40
|
+
// @ts-expect-error intentionally wrong
|
|
41
|
+
now();
|
|
42
|
+
} catch (ex) {
|
|
43
|
+
whoops = true;
|
|
44
|
+
} finally {
|
|
45
|
+
expect(whoops).toBe(true);
|
|
46
|
+
}
|
|
47
|
+
whoops = false;
|
|
48
|
+
try {
|
|
49
|
+
// @ts-expect-error intentionally wrong
|
|
50
|
+
now(locale);
|
|
51
|
+
} catch (ex) {
|
|
52
|
+
whoops = true;
|
|
53
|
+
} finally {
|
|
54
|
+
expect(whoops).toBe(true);
|
|
55
|
+
}
|
|
56
|
+
});
|
|
57
|
+
it('knows when now is', () => {
|
|
58
|
+
// not much of a test, but I need it for coverage stats
|
|
59
|
+
const result = now(locale, timezone);
|
|
60
|
+
expect(result).toBeDefined();
|
|
61
|
+
});
|
|
62
|
+
it("can figure out the browser's timezone", () => {
|
|
63
|
+
const result = browserTimeZone();
|
|
64
|
+
expect(result).toBeDefined();
|
|
65
|
+
});
|
|
66
|
+
it('validates', () => {
|
|
67
|
+
expect(isValid('2018-04-15T23:30:00Z')).toBe(true);
|
|
68
|
+
expect(isValid('2018-04-15T23:30')).toBe(true);
|
|
69
|
+
expect(isValid('2018-04-15')).toBe(true);
|
|
70
|
+
});
|
|
71
|
+
it('parses iso8601', () => {
|
|
72
|
+
const result = parse('2018-04-15T20:30:00-03:00', locale, timezone).toISOString();
|
|
73
|
+
expect(result).toEqual('2018-04-15T23:30:00.000Z');
|
|
74
|
+
});
|
|
75
|
+
it('parses llll', () => {
|
|
76
|
+
const result = parse('Sun, Apr 15, 2018 8:30 PM', locale, timezone).toISOString();
|
|
77
|
+
expect(result).toEqual('2018-04-15T23:30:00.000Z');
|
|
78
|
+
});
|
|
79
|
+
it('parses LLLL', () => {
|
|
80
|
+
const result = parse('Sunday, April 15, 2018 8:30 PM', locale, timezone).toISOString();
|
|
81
|
+
expect(result).toEqual('2018-04-15T23:30:00.000Z');
|
|
82
|
+
});
|
|
83
|
+
it('parses lll', () => {
|
|
84
|
+
const result = parse('Apr 15, 2018 8:30 PM', locale, timezone).toISOString();
|
|
85
|
+
expect(result).toEqual('2018-04-15T23:30:00.000Z');
|
|
86
|
+
});
|
|
87
|
+
it('parses LLL', () => {
|
|
88
|
+
const result = parse('April 15, 2018 8:30 PM', locale, timezone).toISOString();
|
|
89
|
+
expect(result).toEqual('2018-04-15T23:30:00.000Z');
|
|
90
|
+
});
|
|
91
|
+
it('parses ll', () => {
|
|
92
|
+
const result = parse('Apr 15, 2018', locale, timezone).toISOString();
|
|
93
|
+
expect(result).toEqual('2018-04-15T03:00:00.000Z');
|
|
94
|
+
});
|
|
95
|
+
it('parses LL', () => {
|
|
96
|
+
const result = parse('April 15, 2018', locale, timezone).toISOString();
|
|
97
|
+
expect(result).toEqual('2018-04-15T03:00:00.000Z');
|
|
98
|
+
});
|
|
99
|
+
it('parses l', () => {
|
|
100
|
+
const result = parse('4/15/2018', locale, timezone).toISOString();
|
|
101
|
+
expect(result).toEqual('2018-04-15T03:00:00.000Z');
|
|
102
|
+
});
|
|
103
|
+
it('parses L', () => {
|
|
104
|
+
const result = parse('04/15/2018', locale, timezone).toISOString();
|
|
105
|
+
expect(result).toEqual('2018-04-15T03:00:00.000Z');
|
|
106
|
+
});
|
|
107
|
+
it('parses French L', () => {
|
|
108
|
+
const result = parse('15/04/2018', 'fr', timezone).toISOString();
|
|
109
|
+
expect(result).toEqual('2018-04-15T03:00:00.000Z');
|
|
110
|
+
});
|
|
111
|
+
it('parses French LL', () => {
|
|
112
|
+
const result = parse('15 Avril, 2018', 'fr', timezone).toISOString();
|
|
113
|
+
expect(result).toEqual('2018-04-15T03:00:00.000Z');
|
|
114
|
+
});
|
|
115
|
+
it('returns localized string', () => {
|
|
116
|
+
let result = toLocaleString('2018-04-15T13:00Z', 'en', 'UTC', 'LLL');
|
|
117
|
+
expect(result).toEqual('April 15, 2018 1:00 PM');
|
|
118
|
+
result = toLocaleString('2018-04-15T13:00Z', 'fr', 'UTC', 'LLL');
|
|
119
|
+
expect(result).toEqual('15 avril 2018 13:00');
|
|
120
|
+
// iso8601 in given timezone
|
|
121
|
+
result = toLocaleString('2018-04-15T13:00Z', 'fr', 'America/Halifax'); // -3
|
|
122
|
+
expect(result).toEqual('2018-04-15T10:00:00.000-03:00');
|
|
123
|
+
});
|
|
124
|
+
it('calculates the first day of the week', () => {
|
|
125
|
+
// normal case
|
|
126
|
+
const d11 = DateTime.parse('2021-09-15T20:30:00Z', 'hu-hu', 'UTC');
|
|
127
|
+
expect(getFirstDayOfWeek(d11).date()).toEqual(13);
|
|
128
|
+
const d12 = DateTime.parse('2021-09-15T20:30:00Z', 'en-us', 'UTC');
|
|
129
|
+
expect(getFirstDayOfWeek(d12).date()).toEqual(12);
|
|
130
|
+
// date wraps month/year
|
|
131
|
+
const d21 = DateTime.parse('2022-01-01T20:30:00Z', 'hu-hu', 'UTC');
|
|
132
|
+
expect(getFirstDayOfWeek(d21).date()).toEqual(27);
|
|
133
|
+
const d22 = DateTime.parse('2022-01-01T20:30:00Z', 'en-us', 'UTC');
|
|
134
|
+
expect(getFirstDayOfWeek(d22).date()).toEqual(26);
|
|
135
|
+
|
|
136
|
+
// Monday
|
|
137
|
+
const d31 = DateTime.parse('2021-12-05T20:30:00Z', 'hu-hu', 'UTC');
|
|
138
|
+
expect(getFirstDayOfWeek(d31).date()).toEqual(29);
|
|
139
|
+
// Sunday
|
|
140
|
+
const d32 = DateTime.parse('2021-12-05T20:30:00Z', 'en-us', 'UTC');
|
|
141
|
+
expect(getFirstDayOfWeek(d32).date()).toEqual(5);
|
|
142
|
+
// Sunday (but in other systems Friday)
|
|
143
|
+
const d33 = DateTime.parse('2021-12-05T20:30:00Z', 'bn-bd', 'UTC');
|
|
144
|
+
expect(getFirstDayOfWeek(d33).date()).toEqual(5);
|
|
145
|
+
});
|
|
146
|
+
it('calculates the local day names of the week', () => {
|
|
147
|
+
// short names
|
|
148
|
+
expect(getLocalDayNamesOfTheWeek('hu-hu', 'short')).toEqual(['h', 'k', 'sze', 'cs', 'p', 'szo', 'v']);
|
|
149
|
+
expect(getLocalDayNamesOfTheWeek('en-us', 'short')).toEqual(['Su', 'Mo', 'Tu', 'We', 'Th', 'Fr', 'Sa']);
|
|
150
|
+
// long
|
|
151
|
+
expect(getLocalDayNamesOfTheWeek('en-us', 'long')).toEqual(['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday']);
|
|
152
|
+
expect(getLocalDayNamesOfTheWeek('es-mx', 'long')).toEqual(['domingo', 'lunes', 'martes', 'miércoles', 'jueves', 'viernes', 'sábado']);
|
|
153
|
+
});
|
|
154
|
+
});
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* The MIT License (MIT)
|
|
3
|
+
*
|
|
4
|
+
* Copyright (c) 2018 - 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 '@testing-library/jest-dom';
|
|
25
|
+
import { expect } from 'vitest';
|
|
26
|
+
import { I18nPropTypes } from '../I18nPropTypes';
|
|
27
|
+
describe('I18nPropTypes', () => {
|
|
28
|
+
describe('iso8601', () => {
|
|
29
|
+
const iso8601 = I18nPropTypes.iso8601;
|
|
30
|
+
|
|
31
|
+
// Test cases taken from https://www.myintervals.com/blog/2009/05/20/iso-8601-date-validation-that-doesnt-suck/
|
|
32
|
+
it('should not return error when provided with a valid ISO8601 string', () => {
|
|
33
|
+
const props = {
|
|
34
|
+
date: null
|
|
35
|
+
};
|
|
36
|
+
const validISO8601Strings = ['2009-12T12:34', '2009', '2009-05-19', '20090519', '2009123', '2009-05', '2009-123', '2009-222', '2009-001', '2009-W01-1', '2009-W51-1', '2009-W511', '2009-W33', '2009W511', '2009-05-19', '2009-05-19 00:00', '2009-05-19 14', '2009-05-19 14:31', '2009-05-19 14:39:22', '2009-05-19T14:39Z', '2009-W21-2', '2009-W21-2T01:22', '2009-139', '2009-05-19 14:39:22-06:00', '2009-05-19 14:39:22+0600', '2009-05-19 14:39:22-01', '20090621T0545Z', '2007-04-06T00:00', '2007-04-05T24:00', '2010-02-18T16:23:48.5', '2010-02-18T16:23:48,444', '2010-02-18T16:23:48,3-06:00', '2010-02-18T16:23.4', '2010-02-18T16:23,25', '2010-02-18T16:23.33+0600', '2010-02-18T16.23334444', '2010-02-18T16,2283', '2009-05-19 143922.500', '2009-05-19 1439,55'];
|
|
37
|
+
validISO8601Strings.forEach(iso8601string => {
|
|
38
|
+
props.date = iso8601string;
|
|
39
|
+
expect(iso8601(props, 'date', 'TestComponent', '', '')).toBe(null);
|
|
40
|
+
});
|
|
41
|
+
});
|
|
42
|
+
it('should return error when provided with an invalid ISO8601 string', () => {
|
|
43
|
+
const props = {
|
|
44
|
+
date: null
|
|
45
|
+
};
|
|
46
|
+
const invalidISO8601Strings = ['some text', 'June 27, 1990', '200905', '2009367', '2009-', '2007-04-05T24:50', '2009-000', '2009-M511', '2009M511', '2009-05-19T14a39r', '2009-05-19T14:3924', '2009-0519', '2009-05-1914:39', '2009-05-19 14:', '2009-05-19r14:39', '2009-05-19 14a39a22', '200912-01', '2009-05-19 14:39:22+06a00', '2009-05-19 146922.500', '2010-02-18T16.5:23.35:48', '2010-02-18T16:23.35:48', '2010-02-18T16:23.35:48.45', '2009-05-19 14.5.44', '2010-02-18T16:23.33.600', '2010-02-18T16,25:23:48,444'];
|
|
47
|
+
invalidISO8601Strings.forEach(iso8601string => {
|
|
48
|
+
props.date = iso8601string;
|
|
49
|
+
expect(iso8601(props, 'date', 'TestComponent', '', '')).toBeInstanceOf(Error);
|
|
50
|
+
});
|
|
51
|
+
});
|
|
52
|
+
});
|
|
53
|
+
});
|
|
@@ -0,0 +1,52 @@
|
|
|
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 '@testing-library/jest-dom';
|
|
26
|
+
import { expect } from 'vitest';
|
|
27
|
+
import { Locale } from '../Locale';
|
|
28
|
+
describe('browserLocale', () => {
|
|
29
|
+
it('returns the navigator language if a navigator is explicity passed', () => {
|
|
30
|
+
const navigator = {
|
|
31
|
+
language: 'de'
|
|
32
|
+
};
|
|
33
|
+
expect(Locale.browserLocale(navigator)).toBe('de');
|
|
34
|
+
});
|
|
35
|
+
describe('with document lang attribute', () => {
|
|
36
|
+
it('returns the document locale if no navigator is passed', () => {
|
|
37
|
+
const original = document.documentElement.lang;
|
|
38
|
+
document.documentElement.lang = 'fr';
|
|
39
|
+
expect(Locale.browserLocale()).toBe('fr');
|
|
40
|
+
|
|
41
|
+
// Restore global
|
|
42
|
+
document.documentElement.lang = original;
|
|
43
|
+
});
|
|
44
|
+
});
|
|
45
|
+
it('returns the browser locale if no navigator is passed, or "en-US" if no browser locale is set', () => {
|
|
46
|
+
const expectedLanguage = navigator ? navigator.language : 'en-US';
|
|
47
|
+
expect(Locale.browserLocale()).toBe(expectedLanguage);
|
|
48
|
+
});
|
|
49
|
+
it('returns the default "en-US" if navigator is undefined and the DOM is unavailable', () => {
|
|
50
|
+
expect(Locale.browserLocale(null, false)).toBe('en-US');
|
|
51
|
+
});
|
|
52
|
+
});
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
var _dec, _class, _TextDirectionContextConsumerComponent, _div, _TextDirectionContext, _WrapperComponent2, _TextDirectionContext2, _TextDirectionContext3;
|
|
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 { Component } from 'react';
|
|
27
|
+
import ReactDOM from 'react-dom';
|
|
28
|
+
import ReactTestUtils from 'react-dom/test-utils';
|
|
29
|
+
import { render } from '@testing-library/react';
|
|
30
|
+
import '@testing-library/jest-dom';
|
|
31
|
+
import { vi, expect } from 'vitest';
|
|
32
|
+
import { textDirectionContextConsumer } from '../textDirectionContextConsumer';
|
|
33
|
+
import { TextDirectionContext } from '../TextDirectionContext';
|
|
34
|
+
import { jsx as _jsx } from "@emotion/react/jsx-runtime";
|
|
35
|
+
let TextDirectionContextConsumerComponent = (_dec = textDirectionContextConsumer(), _dec(_class = (_TextDirectionContextConsumerComponent = class TextDirectionContextConsumerComponent extends Component {
|
|
36
|
+
render() {
|
|
37
|
+
return _jsx("div", {
|
|
38
|
+
"data-dir": this.props.dir,
|
|
39
|
+
dir: this.props.dir,
|
|
40
|
+
children: this.props.children
|
|
41
|
+
});
|
|
42
|
+
}
|
|
43
|
+
}, _TextDirectionContextConsumerComponent.displayName = "TextDirectionContextConsumerComponent", _TextDirectionContextConsumerComponent)) || _class);
|
|
44
|
+
class WrapperComponent extends Component {
|
|
45
|
+
render() {
|
|
46
|
+
return _div || (_div = _jsx("div", {
|
|
47
|
+
children: _jsx(TextDirectionContextConsumerComponent, {})
|
|
48
|
+
}));
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
WrapperComponent.displayName = "WrapperComponent";
|
|
52
|
+
describe('@textDirectionContextConsumer', () => {
|
|
53
|
+
let consoleErrorMock;
|
|
54
|
+
beforeEach(() => {
|
|
55
|
+
// Mocking console to prevent test output pollution
|
|
56
|
+
consoleErrorMock = vi.spyOn(console, 'error').mockImplementation(() => {});
|
|
57
|
+
});
|
|
58
|
+
afterEach(() => {
|
|
59
|
+
consoleErrorMock.mockRestore();
|
|
60
|
+
});
|
|
61
|
+
it('should take on the direction of the document by default', async () => {
|
|
62
|
+
const _render = render(_TextDirectionContext || (_TextDirectionContext = _jsx(TextDirectionContextConsumerComponent, {}))),
|
|
63
|
+
container = _render.container;
|
|
64
|
+
expect(container.firstChild).toHaveAttribute('data-dir', 'ltr');
|
|
65
|
+
});
|
|
66
|
+
it('can be found and tested with ReactTestUtils', async () => {
|
|
67
|
+
const rootNode = document.createElement('div');
|
|
68
|
+
document.body.appendChild(rootNode);
|
|
69
|
+
|
|
70
|
+
// eslint-disable-next-line react/no-render-return-value
|
|
71
|
+
const rendered = ReactDOM.render(_WrapperComponent2 || (_WrapperComponent2 = _jsx(WrapperComponent, {})), rootNode);
|
|
72
|
+
const foundComponent = ReactTestUtils.findRenderedComponentWithType(rendered, TextDirectionContextConsumerComponent.originalType);
|
|
73
|
+
expect(foundComponent).toBeDefined();
|
|
74
|
+
});
|
|
75
|
+
it('should set the text direction via props', async () => {
|
|
76
|
+
const _render2 = render(_TextDirectionContext2 || (_TextDirectionContext2 = _jsx(TextDirectionContextConsumerComponent, {
|
|
77
|
+
dir: "rtl"
|
|
78
|
+
}))),
|
|
79
|
+
container = _render2.container;
|
|
80
|
+
expect(container.firstChild).toHaveAttribute('data-dir', 'rtl');
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
/* TODO re-enable this test when we allow 'auto' text direction
|
|
84
|
+
it('setting "auto" from context figures out text direction from the text', async () => {
|
|
85
|
+
const subject = await mount(
|
|
86
|
+
<TextDirectionContextConsumerComponent dir="auto">
|
|
87
|
+
<span>
|
|
88
|
+
هذه الفقرة باللغة العربية ، لذا يجب الانتقال من اليمين إلى اليسار.
|
|
89
|
+
</span>
|
|
90
|
+
</TextDirectionContextConsumerComponent>
|
|
91
|
+
)
|
|
92
|
+
expect(
|
|
93
|
+
getComputedStyle(subject.getDOMNode().childNodes[0] as Element).direction
|
|
94
|
+
).to.equal('rtl')
|
|
95
|
+
})
|
|
96
|
+
*/
|
|
97
|
+
|
|
98
|
+
it('should give props preference when context and context are present', async () => {
|
|
99
|
+
const _render3 = render(_TextDirectionContext3 || (_TextDirectionContext3 = _jsx(TextDirectionContext.Provider, {
|
|
100
|
+
value: "ltr",
|
|
101
|
+
children: _jsx(TextDirectionContextConsumerComponent, {
|
|
102
|
+
dir: "rtl"
|
|
103
|
+
})
|
|
104
|
+
}))),
|
|
105
|
+
container = _render3.container;
|
|
106
|
+
expect(container.firstChild).toHaveAttribute('data-dir', 'rtl');
|
|
107
|
+
});
|
|
108
|
+
});
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
var _react = require("react");
|
|
4
|
+
var _react2 = require("@testing-library/react");
|
|
5
|
+
require("@testing-library/jest-dom");
|
|
6
|
+
var _vitest = require("vitest");
|
|
7
|
+
var _index = require("../index");
|
|
8
|
+
var _ApplyLocaleContext = require("../ApplyLocaleContext");
|
|
9
|
+
var _jsxRuntime = require("@emotion/react/jsx-runtime");
|
|
10
|
+
var _ApplyLocale, _ApplyLocale2;
|
|
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
|
+
class LocalizableComponent extends _react.Component {
|
|
35
|
+
render() {
|
|
36
|
+
return (0, _jsxRuntime.jsxs)("div", {
|
|
37
|
+
children: [(0, _jsxRuntime.jsx)("span", {
|
|
38
|
+
children: this.context.locale
|
|
39
|
+
}), (0, _jsxRuntime.jsx)("span", {
|
|
40
|
+
children: this.context.timezone
|
|
41
|
+
})]
|
|
42
|
+
});
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
LocalizableComponent.displayName = "LocalizableComponent";
|
|
46
|
+
LocalizableComponent.contextType = _ApplyLocaleContext.ApplyLocaleContext;
|
|
47
|
+
describe('<ApplyLocale />', async () => {
|
|
48
|
+
it('applies locale context', async () => {
|
|
49
|
+
const _render = (0, _react2.render)(_ApplyLocale || (_ApplyLocale = (0, _jsxRuntime.jsx)(_index.ApplyLocale, {
|
|
50
|
+
locale: "fr",
|
|
51
|
+
children: (0, _jsxRuntime.jsx)(LocalizableComponent, {})
|
|
52
|
+
}))),
|
|
53
|
+
container = _render.container;
|
|
54
|
+
(0, _vitest.expect)(container).toHaveTextContent('fr');
|
|
55
|
+
});
|
|
56
|
+
it('applies timezone context', async () => {
|
|
57
|
+
const _render2 = (0, _react2.render)(_ApplyLocale2 || (_ApplyLocale2 = (0, _jsxRuntime.jsx)(_index.ApplyLocale, {
|
|
58
|
+
timezone: "Europe/Paris",
|
|
59
|
+
children: (0, _jsxRuntime.jsx)(LocalizableComponent, {})
|
|
60
|
+
}))),
|
|
61
|
+
container = _render2.container;
|
|
62
|
+
(0, _vitest.expect)(container).toHaveTextContent('Europe/Paris');
|
|
63
|
+
});
|
|
64
|
+
});
|