@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
|
@@ -0,0 +1,224 @@
|
|
|
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
|
+
|
|
28
|
+
const {
|
|
29
|
+
now,
|
|
30
|
+
isValid,
|
|
31
|
+
browserTimeZone,
|
|
32
|
+
parse,
|
|
33
|
+
toLocaleString,
|
|
34
|
+
getFirstDayOfWeek,
|
|
35
|
+
getLocalDayNamesOfTheWeek
|
|
36
|
+
} = DateTime
|
|
37
|
+
|
|
38
|
+
describe('DateTime', () => {
|
|
39
|
+
const timezone = 'America/Halifax' // -3
|
|
40
|
+
const locale = 'en'
|
|
41
|
+
|
|
42
|
+
it('checks params', () => {
|
|
43
|
+
let whoops = false
|
|
44
|
+
try {
|
|
45
|
+
// @ts-expect-error intentionally wrong
|
|
46
|
+
now()
|
|
47
|
+
} catch (ex) {
|
|
48
|
+
whoops = true
|
|
49
|
+
} finally {
|
|
50
|
+
expect(whoops).toBe(true)
|
|
51
|
+
}
|
|
52
|
+
whoops = false
|
|
53
|
+
try {
|
|
54
|
+
// @ts-expect-error intentionally wrong
|
|
55
|
+
now(locale)
|
|
56
|
+
} catch (ex) {
|
|
57
|
+
whoops = true
|
|
58
|
+
} finally {
|
|
59
|
+
expect(whoops).toBe(true)
|
|
60
|
+
}
|
|
61
|
+
})
|
|
62
|
+
|
|
63
|
+
it('knows when now is', () => {
|
|
64
|
+
// not much of a test, but I need it for coverage stats
|
|
65
|
+
const result = now(locale, timezone)
|
|
66
|
+
expect(result).toBeDefined()
|
|
67
|
+
})
|
|
68
|
+
|
|
69
|
+
it("can figure out the browser's timezone", () => {
|
|
70
|
+
const result = browserTimeZone()
|
|
71
|
+
expect(result).toBeDefined()
|
|
72
|
+
})
|
|
73
|
+
|
|
74
|
+
it('validates', () => {
|
|
75
|
+
expect(isValid('2018-04-15T23:30:00Z')).toBe(true)
|
|
76
|
+
expect(isValid('2018-04-15T23:30')).toBe(true)
|
|
77
|
+
expect(isValid('2018-04-15')).toBe(true)
|
|
78
|
+
})
|
|
79
|
+
|
|
80
|
+
it('parses iso8601', () => {
|
|
81
|
+
const result = parse(
|
|
82
|
+
'2018-04-15T20:30:00-03:00',
|
|
83
|
+
locale,
|
|
84
|
+
timezone
|
|
85
|
+
).toISOString()
|
|
86
|
+
expect(result).toEqual('2018-04-15T23:30:00.000Z')
|
|
87
|
+
})
|
|
88
|
+
|
|
89
|
+
it('parses llll', () => {
|
|
90
|
+
const result = parse(
|
|
91
|
+
'Sun, Apr 15, 2018 8:30 PM',
|
|
92
|
+
locale,
|
|
93
|
+
timezone
|
|
94
|
+
).toISOString()
|
|
95
|
+
expect(result).toEqual('2018-04-15T23:30:00.000Z')
|
|
96
|
+
})
|
|
97
|
+
|
|
98
|
+
it('parses LLLL', () => {
|
|
99
|
+
const result = parse(
|
|
100
|
+
'Sunday, April 15, 2018 8:30 PM',
|
|
101
|
+
locale,
|
|
102
|
+
timezone
|
|
103
|
+
).toISOString()
|
|
104
|
+
expect(result).toEqual('2018-04-15T23:30:00.000Z')
|
|
105
|
+
})
|
|
106
|
+
|
|
107
|
+
it('parses lll', () => {
|
|
108
|
+
const result = parse('Apr 15, 2018 8:30 PM', locale, timezone).toISOString()
|
|
109
|
+
expect(result).toEqual('2018-04-15T23:30:00.000Z')
|
|
110
|
+
})
|
|
111
|
+
|
|
112
|
+
it('parses LLL', () => {
|
|
113
|
+
const result = parse(
|
|
114
|
+
'April 15, 2018 8:30 PM',
|
|
115
|
+
locale,
|
|
116
|
+
timezone
|
|
117
|
+
).toISOString()
|
|
118
|
+
expect(result).toEqual('2018-04-15T23:30:00.000Z')
|
|
119
|
+
})
|
|
120
|
+
|
|
121
|
+
it('parses ll', () => {
|
|
122
|
+
const result = parse('Apr 15, 2018', locale, timezone).toISOString()
|
|
123
|
+
expect(result).toEqual('2018-04-15T03:00:00.000Z')
|
|
124
|
+
})
|
|
125
|
+
|
|
126
|
+
it('parses LL', () => {
|
|
127
|
+
const result = parse('April 15, 2018', locale, timezone).toISOString()
|
|
128
|
+
expect(result).toEqual('2018-04-15T03:00:00.000Z')
|
|
129
|
+
})
|
|
130
|
+
|
|
131
|
+
it('parses l', () => {
|
|
132
|
+
const result = parse('4/15/2018', locale, timezone).toISOString()
|
|
133
|
+
expect(result).toEqual('2018-04-15T03:00:00.000Z')
|
|
134
|
+
})
|
|
135
|
+
|
|
136
|
+
it('parses L', () => {
|
|
137
|
+
const result = parse('04/15/2018', locale, timezone).toISOString()
|
|
138
|
+
expect(result).toEqual('2018-04-15T03:00:00.000Z')
|
|
139
|
+
})
|
|
140
|
+
|
|
141
|
+
it('parses French L', () => {
|
|
142
|
+
const result = parse('15/04/2018', 'fr', timezone).toISOString()
|
|
143
|
+
expect(result).toEqual('2018-04-15T03:00:00.000Z')
|
|
144
|
+
})
|
|
145
|
+
|
|
146
|
+
it('parses French LL', () => {
|
|
147
|
+
const result = parse('15 Avril, 2018', 'fr', timezone).toISOString()
|
|
148
|
+
expect(result).toEqual('2018-04-15T03:00:00.000Z')
|
|
149
|
+
})
|
|
150
|
+
|
|
151
|
+
it('returns localized string', () => {
|
|
152
|
+
let result = toLocaleString('2018-04-15T13:00Z', 'en', 'UTC', 'LLL')
|
|
153
|
+
expect(result).toEqual('April 15, 2018 1:00 PM')
|
|
154
|
+
result = toLocaleString('2018-04-15T13:00Z', 'fr', 'UTC', 'LLL')
|
|
155
|
+
expect(result).toEqual('15 avril 2018 13:00')
|
|
156
|
+
// iso8601 in given timezone
|
|
157
|
+
result = toLocaleString('2018-04-15T13:00Z', 'fr', 'America/Halifax') // -3
|
|
158
|
+
expect(result).toEqual('2018-04-15T10:00:00.000-03:00')
|
|
159
|
+
})
|
|
160
|
+
|
|
161
|
+
it('calculates the first day of the week', () => {
|
|
162
|
+
// normal case
|
|
163
|
+
const d11 = DateTime.parse('2021-09-15T20:30:00Z', 'hu-hu', 'UTC')
|
|
164
|
+
expect(getFirstDayOfWeek(d11).date()).toEqual(13)
|
|
165
|
+
const d12 = DateTime.parse('2021-09-15T20:30:00Z', 'en-us', 'UTC')
|
|
166
|
+
expect(getFirstDayOfWeek(d12).date()).toEqual(12)
|
|
167
|
+
// date wraps month/year
|
|
168
|
+
const d21 = DateTime.parse('2022-01-01T20:30:00Z', 'hu-hu', 'UTC')
|
|
169
|
+
expect(getFirstDayOfWeek(d21).date()).toEqual(27)
|
|
170
|
+
const d22 = DateTime.parse('2022-01-01T20:30:00Z', 'en-us', 'UTC')
|
|
171
|
+
expect(getFirstDayOfWeek(d22).date()).toEqual(26)
|
|
172
|
+
|
|
173
|
+
// Monday
|
|
174
|
+
const d31 = DateTime.parse('2021-12-05T20:30:00Z', 'hu-hu', 'UTC')
|
|
175
|
+
expect(getFirstDayOfWeek(d31).date()).toEqual(29)
|
|
176
|
+
// Sunday
|
|
177
|
+
const d32 = DateTime.parse('2021-12-05T20:30:00Z', 'en-us', 'UTC')
|
|
178
|
+
expect(getFirstDayOfWeek(d32).date()).toEqual(5)
|
|
179
|
+
// Sunday (but in other systems Friday)
|
|
180
|
+
const d33 = DateTime.parse('2021-12-05T20:30:00Z', 'bn-bd', 'UTC')
|
|
181
|
+
expect(getFirstDayOfWeek(d33).date()).toEqual(5)
|
|
182
|
+
})
|
|
183
|
+
|
|
184
|
+
it('calculates the local day names of the week', () => {
|
|
185
|
+
// short names
|
|
186
|
+
expect(getLocalDayNamesOfTheWeek('hu-hu', 'short')).toEqual([
|
|
187
|
+
'h',
|
|
188
|
+
'k',
|
|
189
|
+
'sze',
|
|
190
|
+
'cs',
|
|
191
|
+
'p',
|
|
192
|
+
'szo',
|
|
193
|
+
'v'
|
|
194
|
+
])
|
|
195
|
+
expect(getLocalDayNamesOfTheWeek('en-us', 'short')).toEqual([
|
|
196
|
+
'Su',
|
|
197
|
+
'Mo',
|
|
198
|
+
'Tu',
|
|
199
|
+
'We',
|
|
200
|
+
'Th',
|
|
201
|
+
'Fr',
|
|
202
|
+
'Sa'
|
|
203
|
+
])
|
|
204
|
+
// long
|
|
205
|
+
expect(getLocalDayNamesOfTheWeek('en-us', 'long')).toEqual([
|
|
206
|
+
'Sunday',
|
|
207
|
+
'Monday',
|
|
208
|
+
'Tuesday',
|
|
209
|
+
'Wednesday',
|
|
210
|
+
'Thursday',
|
|
211
|
+
'Friday',
|
|
212
|
+
'Saturday'
|
|
213
|
+
])
|
|
214
|
+
expect(getLocalDayNamesOfTheWeek('es-mx', 'long')).toEqual([
|
|
215
|
+
'domingo',
|
|
216
|
+
'lunes',
|
|
217
|
+
'martes',
|
|
218
|
+
'miércoles',
|
|
219
|
+
'jueves',
|
|
220
|
+
'viernes',
|
|
221
|
+
'sábado'
|
|
222
|
+
])
|
|
223
|
+
})
|
|
224
|
+
})
|
|
@@ -0,0 +1,119 @@
|
|
|
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
|
+
|
|
28
|
+
describe('I18nPropTypes', () => {
|
|
29
|
+
describe('iso8601', () => {
|
|
30
|
+
const { iso8601 } = I18nPropTypes
|
|
31
|
+
|
|
32
|
+
// Test cases taken from https://www.myintervals.com/blog/2009/05/20/iso-8601-date-validation-that-doesnt-suck/
|
|
33
|
+
it('should not return error when provided with a valid ISO8601 string', () => {
|
|
34
|
+
const props: { date: string | null } = { date: null }
|
|
35
|
+
const validISO8601Strings = [
|
|
36
|
+
'2009-12T12:34',
|
|
37
|
+
'2009',
|
|
38
|
+
'2009-05-19',
|
|
39
|
+
'20090519',
|
|
40
|
+
'2009123',
|
|
41
|
+
'2009-05',
|
|
42
|
+
'2009-123',
|
|
43
|
+
'2009-222',
|
|
44
|
+
'2009-001',
|
|
45
|
+
'2009-W01-1',
|
|
46
|
+
'2009-W51-1',
|
|
47
|
+
'2009-W511',
|
|
48
|
+
'2009-W33',
|
|
49
|
+
'2009W511',
|
|
50
|
+
'2009-05-19',
|
|
51
|
+
'2009-05-19 00:00',
|
|
52
|
+
'2009-05-19 14',
|
|
53
|
+
'2009-05-19 14:31',
|
|
54
|
+
'2009-05-19 14:39:22',
|
|
55
|
+
'2009-05-19T14:39Z',
|
|
56
|
+
'2009-W21-2',
|
|
57
|
+
'2009-W21-2T01:22',
|
|
58
|
+
'2009-139',
|
|
59
|
+
'2009-05-19 14:39:22-06:00',
|
|
60
|
+
'2009-05-19 14:39:22+0600',
|
|
61
|
+
'2009-05-19 14:39:22-01',
|
|
62
|
+
'20090621T0545Z',
|
|
63
|
+
'2007-04-06T00:00',
|
|
64
|
+
'2007-04-05T24:00',
|
|
65
|
+
'2010-02-18T16:23:48.5',
|
|
66
|
+
'2010-02-18T16:23:48,444',
|
|
67
|
+
'2010-02-18T16:23:48,3-06:00',
|
|
68
|
+
'2010-02-18T16:23.4',
|
|
69
|
+
'2010-02-18T16:23,25',
|
|
70
|
+
'2010-02-18T16:23.33+0600',
|
|
71
|
+
'2010-02-18T16.23334444',
|
|
72
|
+
'2010-02-18T16,2283',
|
|
73
|
+
'2009-05-19 143922.500',
|
|
74
|
+
'2009-05-19 1439,55'
|
|
75
|
+
]
|
|
76
|
+
validISO8601Strings.forEach((iso8601string) => {
|
|
77
|
+
props.date = iso8601string
|
|
78
|
+
expect(iso8601(props, 'date', 'TestComponent', '', '')).toBe(null)
|
|
79
|
+
})
|
|
80
|
+
})
|
|
81
|
+
|
|
82
|
+
it('should return error when provided with an invalid ISO8601 string', () => {
|
|
83
|
+
const props: { date: string | null } = { date: null }
|
|
84
|
+
const invalidISO8601Strings = [
|
|
85
|
+
'some text',
|
|
86
|
+
'June 27, 1990',
|
|
87
|
+
'200905',
|
|
88
|
+
'2009367',
|
|
89
|
+
'2009-',
|
|
90
|
+
'2007-04-05T24:50',
|
|
91
|
+
'2009-000',
|
|
92
|
+
'2009-M511',
|
|
93
|
+
'2009M511',
|
|
94
|
+
'2009-05-19T14a39r',
|
|
95
|
+
'2009-05-19T14:3924',
|
|
96
|
+
'2009-0519',
|
|
97
|
+
'2009-05-1914:39',
|
|
98
|
+
'2009-05-19 14:',
|
|
99
|
+
'2009-05-19r14:39',
|
|
100
|
+
'2009-05-19 14a39a22',
|
|
101
|
+
'200912-01',
|
|
102
|
+
'2009-05-19 14:39:22+06a00',
|
|
103
|
+
'2009-05-19 146922.500',
|
|
104
|
+
'2010-02-18T16.5:23.35:48',
|
|
105
|
+
'2010-02-18T16:23.35:48',
|
|
106
|
+
'2010-02-18T16:23.35:48.45',
|
|
107
|
+
'2009-05-19 14.5.44',
|
|
108
|
+
'2010-02-18T16:23.33.600',
|
|
109
|
+
'2010-02-18T16,25:23:48,444'
|
|
110
|
+
]
|
|
111
|
+
invalidISO8601Strings.forEach((iso8601string) => {
|
|
112
|
+
props.date = iso8601string
|
|
113
|
+
expect(iso8601(props, 'date', 'TestComponent', '', '')).toBeInstanceOf(
|
|
114
|
+
Error
|
|
115
|
+
)
|
|
116
|
+
})
|
|
117
|
+
})
|
|
118
|
+
})
|
|
119
|
+
})
|
|
@@ -0,0 +1,55 @@
|
|
|
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
|
+
|
|
29
|
+
describe('browserLocale', () => {
|
|
30
|
+
it('returns the navigator language if a navigator is explicity passed', () => {
|
|
31
|
+
const navigator = { language: 'de' }
|
|
32
|
+
expect(Locale.browserLocale(navigator)).toBe('de')
|
|
33
|
+
})
|
|
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
|
+
|
|
39
|
+
document.documentElement.lang = 'fr'
|
|
40
|
+
expect(Locale.browserLocale()).toBe('fr')
|
|
41
|
+
|
|
42
|
+
// Restore global
|
|
43
|
+
document.documentElement.lang = original
|
|
44
|
+
})
|
|
45
|
+
})
|
|
46
|
+
|
|
47
|
+
it('returns the browser locale if no navigator is passed, or "en-US" if no browser locale is set', () => {
|
|
48
|
+
const expectedLanguage = navigator ? navigator.language : 'en-US'
|
|
49
|
+
expect(Locale.browserLocale()).toBe(expectedLanguage)
|
|
50
|
+
})
|
|
51
|
+
|
|
52
|
+
it('returns the default "en-US" if navigator is undefined and the DOM is unavailable', () => {
|
|
53
|
+
expect(Locale.browserLocale(null, false)).toBe('en-US')
|
|
54
|
+
})
|
|
55
|
+
})
|
|
@@ -0,0 +1,125 @@
|
|
|
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 { Component } from 'react'
|
|
26
|
+
import ReactDOM from 'react-dom'
|
|
27
|
+
import ReactTestUtils from 'react-dom/test-utils'
|
|
28
|
+
import { render } from '@testing-library/react'
|
|
29
|
+
import '@testing-library/jest-dom'
|
|
30
|
+
import { vi, expect } from 'vitest'
|
|
31
|
+
import type { MockInstance } from 'vitest'
|
|
32
|
+
|
|
33
|
+
import {
|
|
34
|
+
textDirectionContextConsumer,
|
|
35
|
+
TextDirectionContextConsumerProps
|
|
36
|
+
} from '../textDirectionContextConsumer'
|
|
37
|
+
import { TextDirectionContext } from '../TextDirectionContext'
|
|
38
|
+
|
|
39
|
+
@textDirectionContextConsumer()
|
|
40
|
+
class TextDirectionContextConsumerComponent extends Component<TextDirectionContextConsumerProps> {
|
|
41
|
+
render() {
|
|
42
|
+
return (
|
|
43
|
+
<div data-dir={this.props.dir} dir={this.props.dir}>
|
|
44
|
+
{this.props.children}
|
|
45
|
+
</div>
|
|
46
|
+
)
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
class WrapperComponent extends Component {
|
|
51
|
+
render() {
|
|
52
|
+
return (
|
|
53
|
+
<div>
|
|
54
|
+
<TextDirectionContextConsumerComponent />
|
|
55
|
+
</div>
|
|
56
|
+
)
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
describe('@textDirectionContextConsumer', () => {
|
|
61
|
+
let consoleErrorMock: ReturnType<typeof vi.spyOn>
|
|
62
|
+
|
|
63
|
+
beforeEach(() => {
|
|
64
|
+
// Mocking console to prevent test output pollution
|
|
65
|
+
consoleErrorMock = vi
|
|
66
|
+
.spyOn(console, 'error')
|
|
67
|
+
.mockImplementation(() => {}) as MockInstance
|
|
68
|
+
})
|
|
69
|
+
|
|
70
|
+
afterEach(() => {
|
|
71
|
+
consoleErrorMock.mockRestore()
|
|
72
|
+
})
|
|
73
|
+
|
|
74
|
+
it('should take on the direction of the document by default', async () => {
|
|
75
|
+
const { container } = render(<TextDirectionContextConsumerComponent />)
|
|
76
|
+
|
|
77
|
+
expect(container.firstChild).toHaveAttribute('data-dir', 'ltr')
|
|
78
|
+
})
|
|
79
|
+
|
|
80
|
+
it('can be found and tested with ReactTestUtils', async () => {
|
|
81
|
+
const rootNode = document.createElement('div')
|
|
82
|
+
|
|
83
|
+
document.body.appendChild(rootNode)
|
|
84
|
+
|
|
85
|
+
// eslint-disable-next-line react/no-render-return-value
|
|
86
|
+
const rendered = ReactDOM.render(<WrapperComponent />, rootNode)
|
|
87
|
+
const foundComponent = ReactTestUtils.findRenderedComponentWithType(
|
|
88
|
+
rendered as any,
|
|
89
|
+
(TextDirectionContextConsumerComponent as any).originalType
|
|
90
|
+
)
|
|
91
|
+
|
|
92
|
+
expect(foundComponent).toBeDefined()
|
|
93
|
+
})
|
|
94
|
+
|
|
95
|
+
it('should set the text direction via props', async () => {
|
|
96
|
+
const { container } = render(
|
|
97
|
+
<TextDirectionContextConsumerComponent dir="rtl" />
|
|
98
|
+
)
|
|
99
|
+
expect(container.firstChild).toHaveAttribute('data-dir', 'rtl')
|
|
100
|
+
})
|
|
101
|
+
|
|
102
|
+
/* TODO re-enable this test when we allow 'auto' text direction
|
|
103
|
+
it('setting "auto" from context figures out text direction from the text', async () => {
|
|
104
|
+
const subject = await mount(
|
|
105
|
+
<TextDirectionContextConsumerComponent dir="auto">
|
|
106
|
+
<span>
|
|
107
|
+
هذه الفقرة باللغة العربية ، لذا يجب الانتقال من اليمين إلى اليسار.
|
|
108
|
+
</span>
|
|
109
|
+
</TextDirectionContextConsumerComponent>
|
|
110
|
+
)
|
|
111
|
+
expect(
|
|
112
|
+
getComputedStyle(subject.getDOMNode().childNodes[0] as Element).direction
|
|
113
|
+
).to.equal('rtl')
|
|
114
|
+
})
|
|
115
|
+
*/
|
|
116
|
+
|
|
117
|
+
it('should give props preference when context and context are present', async () => {
|
|
118
|
+
const { container } = render(
|
|
119
|
+
<TextDirectionContext.Provider value="ltr">
|
|
120
|
+
<TextDirectionContextConsumerComponent dir="rtl" />
|
|
121
|
+
</TextDirectionContext.Provider>
|
|
122
|
+
)
|
|
123
|
+
expect(container.firstChild).toHaveAttribute('data-dir', 'rtl')
|
|
124
|
+
})
|
|
125
|
+
})
|
package/tsconfig.build.json
CHANGED
|
@@ -11,9 +11,8 @@
|
|
|
11
11
|
"path": "../ui-babel-preset/tsconfig.build.json"
|
|
12
12
|
},
|
|
13
13
|
{
|
|
14
|
-
"path": "../
|
|
14
|
+
"path": "../shared-types/tsconfig.build.json"
|
|
15
15
|
},
|
|
16
|
-
{ "path": "../shared-types/tsconfig.build.json" },
|
|
17
16
|
{
|
|
18
17
|
"path": "../ui-decorator/tsconfig.build.json"
|
|
19
18
|
},
|