Skip to content

Commit

Permalink
add support for plugin config
Browse files Browse the repository at this point in the history
  • Loading branch information
rogeliog committed Jul 4, 2018
1 parent 23f22d4 commit 15d88ac
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 6 deletions.
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,22 @@ module.exports = {
};
```

### Configuring your key and prompt name

```js
module.exports = {
watchPlugins: [
[
'jest-watch-typeahead/filename',
{
key: 'k',
prompt: 'do something with my custom prompt',
},
],
],
};
```

### Run Jest in watch mode

```bash
Expand Down
12 changes: 12 additions & 0 deletions src/__tests__/file_name_pattern_prompt.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,3 +79,15 @@ it('can select a pattern that matches multiple files', async () => {
testPathPattern: 'fi',
});
});

it('can configure the key and prompt', async () => {
const { plugin } = pluginTester(FileNamePlugin, {
key: 'l',
prompt: 'have a custom prompt',
});

expect(plugin.getUsageInfo()).toEqual({
key: 'l',
prompt: 'have a custom prompt',
});
});
4 changes: 2 additions & 2 deletions src/__tests__/pluginTester.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ jest.mock('ansi-escapes', () => ({
cursorTo: (x, y) => `[MOCK - cursorTo(${x}, ${y})]`,
}));

const pluginTester = Plugin => {
const pluginTester = (Plugin, config = {}) => {
const stdout = { columns: 80, write: jest.fn() };
const jestHooks = new JestHook();
const plugin = new Plugin({ stdout });
const plugin = new Plugin({ stdout, config });
plugin.apply(jestHooks.getSubscriber());

const type = (...keys) => keys.forEach(key => plugin.onKey(key));
Expand Down
12 changes: 12 additions & 0 deletions src/__tests__/test_name_pattern_prompt.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,3 +80,15 @@ it('can select a pattern that matches multiple tests', async () => {
testNamePattern: 'fo',
});
});

it('can configure the key and prompt', async () => {
const { plugin } = pluginTester(TestNamePlugin, {
key: 'l',
prompt: 'have a custom prompt',
});

expect(plugin.getUsageInfo()).toEqual({
key: 'l',
prompt: 'have a custom prompt',
});
});
13 changes: 11 additions & 2 deletions src/file_name_plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,32 @@ import FileNamePatternPrompt, {
type SearchSources,
} from './file_name_pattern_prompt';

type PluginConfig = {
key?: string,
prompt?: string,
};

class FileNamePlugin {
_stdin: stream$Readable | tty$ReadStream;
_stdout: stream$Writable | tty$WriteStream;
_prompt: Prompt;
_projects: SearchSources;
_config: PluginConfig;

constructor({
stdin,
stdout,
config,
}: {
stdin: stream$Readable | tty$ReadStream,
stdout: stream$Writable | tty$WriteStream,
config: PluginConfig,
}) {
this._stdin = stdin;
this._stdout = stdout;
this._prompt = new Prompt();
this._projects = [];
this._config = config;
}

apply(jestHooks: Object) {
Expand All @@ -48,8 +57,8 @@ class FileNamePlugin {
// eslint-disable-next-line class-methods-use-this
getUsageInfo() {
return {
key: 'p',
prompt: 'filter by a filename regex pattern',
key: this._config.key || 'p',
prompt: this._config.prompt || 'filter by a filename regex pattern',
};
}
}
Expand Down
13 changes: 11 additions & 2 deletions src/test_name_plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,32 @@ import TestNamePatternPrompt, {
type TestResult,
} from './test_name_pattern_prompt';

type PluginConfig = {
key?: string,
prompt?: string,
};

class TestNamePlugin {
_stdin: stream$Readable | tty$ReadStream;
_stdout: stream$Writable | tty$WriteStream;
_prompt: Prompt;
_testResults: Array<TestResult>;
_config: PluginConfig;

constructor({
stdin,
stdout,
config,
}: {
stdin: stream$Readable | tty$ReadStream,
stdout: stream$Writable | tty$WriteStream,
config: PluginConfig,
}) {
this._stdin = stdin;
this._stdout = stdout;
this._prompt = new Prompt();
this._testResults = [];
this._config = config;
}

apply(jestHooks: Object) {
Expand All @@ -48,8 +57,8 @@ class TestNamePlugin {
// eslint-disable-next-line class-methods-use-this
getUsageInfo() {
return {
key: 't',
prompt: 'filter by a test name regex pattern',
key: this._config.key || 't',
prompt: this._config.prompt || 'filter by a test name regex pattern',
};
}
}
Expand Down

0 comments on commit 15d88ac

Please sign in to comment.