|
| 1 | +import {promisify} from 'util'; |
| 2 | +import {execFile} from 'child_process'; |
1 | 3 | import test from 'ava'; |
2 | 4 | import camelcaseKeys from '.'; |
3 | 5 |
|
| 6 | +const execFilePromise = promisify(execFile); |
| 7 | + |
4 | 8 | test('main', t => { |
5 | 9 | t.true(camelcaseKeys({'foo-bar': true}).fooBar); |
6 | 10 | }); |
@@ -106,3 +110,38 @@ test('handle array of non-objects with `deep` option', t => { |
106 | 110 | input |
107 | 111 | ); |
108 | 112 | }); |
| 113 | + |
| 114 | +test('use locale independent camel-case transformation', async t => { |
| 115 | + const input = {'user-id': 123}; |
| 116 | + t.deepEqual( |
| 117 | + // Execute the library with Turkish locale. |
| 118 | + // A locale dependent implementation would return `{userİd: 123}`. |
| 119 | + // See https://github.com/sindresorhus/camelcase-keys/issues/81 |
| 120 | + await runInTestProcess([input], {env: {...process.env, LC_ALL: 'tr'}}), |
| 121 | + {userId: 123} |
| 122 | + ); |
| 123 | +}); |
| 124 | + |
| 125 | +/** |
| 126 | +Executes the library with the given arguments and resolves with the parsed result. |
| 127 | +
|
| 128 | +Input and output is serialized via `JSON.stringify()` and `JSON.parse()`. |
| 129 | +*/ |
| 130 | +const runInTestProcess = async (camelcaseKeysArgs, childProcessOptions = {}) => { |
| 131 | + const {stdout, stderr} = await execFilePromise( |
| 132 | + process.execPath, |
| 133 | + ['./fixtures/child-process-for-test.js', JSON.stringify(camelcaseKeysArgs)], |
| 134 | + childProcessOptions |
| 135 | + ); |
| 136 | + |
| 137 | + if (stderr) { |
| 138 | + throw new Error(stderr); |
| 139 | + } |
| 140 | + |
| 141 | + try { |
| 142 | + return JSON.parse(stdout); |
| 143 | + } catch (error) { |
| 144 | + error.message = `Error parsing "${stdout}" as JSON: ${error.message}`; |
| 145 | + throw error; |
| 146 | + } |
| 147 | +}; |
0 commit comments