Skip to content

Commit

Permalink
Make matching case insensitive (#18)
Browse files Browse the repository at this point in the history
* Make matching case insensitive

* Update changelog

* change filter implementations to return early and use reduce
  • Loading branch information
rogeliog authored Jul 8, 2018
1 parent 28c9944 commit 11d8073
Show file tree
Hide file tree
Showing 7 changed files with 89 additions and 25 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
* Add support for plugin config ([#13](https://github.com/jest-community/jest-watch-typeahead/pull/13))

### Fixes
* Make matching case insensitive ([#18](https://github.com/jest-community/jest-watch-typeahead/pull/18))
* fix: migrate to use jest-watcher ([#6](https://github.com/jest-community/jest-watch-typeahead/pull/6))


Expand Down
17 changes: 17 additions & 0 deletions src/file_name_plugin/__tests__/__snapshots__/plugin.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,23 @@ exports[`can use arrows to select a specific file 1`] = `
[MOCK - cursorRestorePosition]
`;

exports[`file matching is case insensitive 1`] = `
pattern › fI
[MOCK - cursorSavePosition]
Pattern matches 2 files
› src/file-1.js
› src/file-2.js
[MOCK - cursorTo(13, 5)]
[MOCK - cursorRestorePosition]
`;

exports[`shows the correct initial state 1`] = `
[MOCK - cursorHide]
[MOCK - clearScreen]
Expand Down
19 changes: 19 additions & 0 deletions src/file_name_plugin/__tests__/plugin.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,3 +91,22 @@ it('can configure the key and prompt', async () => {
prompt: 'have a custom prompt',
});
});

it('file matching is case insensitive', async () => {
const {
stdout,
hookEmitter,
updateConfigAndRun,
plugin,
type,
} = pluginTester(FileNamePlugin);

hookEmitter.onFileChange({ projects });
const runPromise = plugin.run({}, updateConfigAndRun);
type('f');
stdout.write.mockReset();
type('I');
expect(stdout.write.mock.calls.join('\n')).toMatchSnapshot();
type(KEYS.ENTER);
await runPromise;
});
23 changes: 9 additions & 14 deletions src/file_name_plugin/prompt.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,22 +88,17 @@ export default class FileNamePatternPrompt extends PatternPrompt {
try {
regex = new RegExp(pattern, 'i');
} catch (e) {
regex = null;
return [];
}

let tests = [];
if (regex) {
this._searchSources.forEach(({ testPaths, config }) => {
tests = tests.concat(
testPaths.filter(testPath => testPath.match(pattern)).map(path => ({
path,
context: { config },
})),
);
});
}

return tests;
return this._searchSources.reduce((tests, { testPaths, config }) => {
return tests.concat(
testPaths.filter(testPath => regex.test(testPath)).map(path => ({
path,
context: { config },
})),
);
}, []);
}

updateSearchSources(searchSources: SearchSources) {
Expand Down
17 changes: 17 additions & 0 deletions src/test_name_plugin/__tests__/__snapshots__/plugin.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -119,3 +119,20 @@ Pattern Mode Usage
[MOCK - cursorTo(11, 5)]
[MOCK - cursorRestorePosition]
`;

exports[`test matching is case insensitive 1`] = `
pattern › fO
[MOCK - cursorSavePosition]
Pattern matches 2 tests from cached test suites
› foo 1
› foo 2
[MOCK - cursorTo(13, 5)]
[MOCK - cursorRestorePosition]
`;
19 changes: 19 additions & 0 deletions src/test_name_plugin/__tests__/plugin.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,3 +92,22 @@ it('can configure the key and prompt', async () => {
prompt: 'have a custom prompt',
});
});

it('test matching is case insensitive', async () => {
const {
stdout,
hookEmitter,
updateConfigAndRun,
plugin,
type,
} = pluginTester(TestNamePlugin);

hookEmitter.onTestRunComplete({ testResults });
const runPromise = plugin.run({}, updateConfigAndRun);
type('f');
stdout.write.mockReset();
type('O');
expect(stdout.write.mock.calls.join('\n')).toMatchSnapshot();
type(KEYS.ENTER);
await runPromise;
});
18 changes: 7 additions & 11 deletions src/test_name_plugin/prompt.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,17 +83,13 @@ class TestNamePatternPrompt extends PatternPrompt {
return [];
}

const matchedTests = [];

this._cachedTestResults.forEach(({ testResults }) =>
testResults.forEach(({ title }) => {
if (regex.test(title)) {
matchedTests.push(title);
}
}),
);

return matchedTests;
return this._cachedTestResults.reduce((matchedTests, { testResults }) => {
return matchedTests.concat(
testResults
.filter(({ title }) => regex.test(title))
.map(({ title }) => title),
);
}, []);
}

updateCachedTestResults(testResults: Array<TestResult> = []) {
Expand Down

0 comments on commit 11d8073

Please sign in to comment.