Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for plugin config #13

Merged
merged 2 commits into from
Jul 6, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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',
});
});
18 changes: 13 additions & 5 deletions src/file_name_plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,35 @@ 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;
_usageInfo: { key: string, prompt: string };

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._usageInfo = {
key: config.key || 'p',
prompt: config.prompt || 'filter by a filename regex pattern',
};
}

apply(jestHooks: Object) {
Expand All @@ -45,12 +57,8 @@ class FileNamePlugin {
});
}

// eslint-disable-next-line class-methods-use-this
getUsageInfo() {
return {
key: 'p',
prompt: 'filter by a filename regex pattern',
};
return this._usageInfo;
}
}

Expand Down
18 changes: 13 additions & 5 deletions src/test_name_plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,35 @@ 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>;
_usageInfo: { key: string, prompt: string };

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._usageInfo = {
key: config.key || 't',
prompt: config.prompt || 'filter by a test name regex pattern',
};
}

apply(jestHooks: Object) {
Expand All @@ -45,12 +57,8 @@ class TestNamePlugin {
});
}

// eslint-disable-next-line class-methods-use-this
getUsageInfo() {
return {
key: 't',
prompt: 'filter by a test name regex pattern',
};
return this._usageInfo;
}
}

Expand Down