1import { ExpoConfig } from '@expo/config';
2import * as ConfigPlugins from '@expo/config-plugins';
3
4import { createLegacyPlugin } from '../createLegacyPlugin';
5
6jest.mock('@expo/config-plugins', () => {
7  const plugins = jest.requireActual('@expo/config-plugins');
8  return {
9    ...plugins,
10    withStaticPlugin: jest.fn(plugins.withStaticPlugin),
11  };
12});
13
14describe(createLegacyPlugin, () => {
15  it(`uses fallback`, () => {
16    const fallback = jest.fn((config) => config);
17    const withPlugin = createLegacyPlugin({
18      packageName: 'expo-foobar',
19      fallback,
20    });
21    let config: ExpoConfig = { slug: '', name: '', _internal: { projectRoot: '/' } };
22    config = withPlugin(config);
23    expect(fallback).toBeCalledTimes(1);
24    expect(config._internal.pluginHistory).toStrictEqual({
25      'expo-foobar': {
26        name: 'expo-foobar',
27        version: 'UNVERSIONED',
28      },
29    });
30    // Only invokes the fallback once
31    config = withPlugin(config);
32    expect(fallback).toBeCalledTimes(1);
33  });
34
35  it(`uses versioned plugin instead of fallback`, () => {
36    require('@expo/config-plugins').withStaticPlugin = jest.fn();
37    const fallback = jest.fn((config) => config);
38    const withPlugin = createLegacyPlugin({
39      packageName: 'expo-foobar',
40      fallback,
41    });
42    const config: ExpoConfig = { slug: '', name: '', _internal: { projectRoot: '/' } };
43    withPlugin(config);
44    expect(ConfigPlugins.withStaticPlugin).toBeCalledTimes(1);
45    expect(fallback).toBeCalledTimes(0);
46  });
47});
48