1import { ConfigPlugin } from '../../Plugin.types';
2import { withPlugins } from '../withPlugins';
3import { createRunOncePlugin, withRunOnce } from '../withRunOnce';
4
5describe(withRunOnce, () => {
6  it('runs plugins multiple times without withRunOnce', () => {
7    const pluginA: ConfigPlugin = jest.fn((config) => config);
8
9    withPlugins({ extra: [], _internal: { projectRoot: '.' } } as any, [
10      // Prove unsafe runs as many times as it was added
11      pluginA,
12      pluginA,
13    ]);
14
15    // Unsafe runs multiple times
16    expect(pluginA).toBeCalledTimes(2);
17  });
18
19  it('prevents running different plugins with same id', () => {
20    const pluginA: ConfigPlugin = jest.fn((config) => config);
21    const pluginB: ConfigPlugin = jest.fn((config) => config);
22
23    const pluginId = 'foo';
24
25    const safePluginA = createRunOncePlugin(pluginA, pluginId);
26    // A different plugin with the same ID as (A), this proves
27    // that different plugins can be prevented when using the same ID.
28    const safePluginB = createRunOncePlugin(pluginB, pluginId);
29
30    withPlugins({ extra: [], _internal: { projectRoot: '.' } } as any, [
31      // Run plugin twice
32      safePluginA,
33      safePluginB,
34    ]);
35
36    // Prove that each plugin is only run once
37    expect(pluginA).toBeCalledTimes(1);
38    expect(pluginB).toBeCalledTimes(0);
39  });
40
41  it('prevents running the same plugin twice', () => {
42    const pluginA: ConfigPlugin = jest.fn((config) => config);
43    const pluginId = 'foo';
44
45    const safePluginA = createRunOncePlugin(pluginA, pluginId);
46
47    withPlugins({ extra: [], _internal: { projectRoot: '.' } } as any, [
48      // Run plugin twice
49      safePluginA,
50      safePluginA,
51    ]);
52
53    // Prove that each plugin is only run once
54    expect(pluginA).toBeCalledTimes(1);
55  });
56});
57