import { expect } from 'chai'; import { executeBabel, baseConfig } from './helpers.mjs'; /* eslint-disable no-template-curly-in-string */ const testConfig = { ...baseConfig, }; describe('babel-plugin-extend-docs', () => { it('replaces src class imports (1)', () => { const code = `import { LionInput } from '@lion/input';`; const output = `import { WolfInput } from "wolf-web/input";`; expect(executeBabel(code, testConfig)).to.equal(output); }); it('renames classes everywhere', () => { const code = [ `import { LionInput } from '@lion/input';`, `class Foo extends LionInput {}`, ].join('\n'); const output = [ `import { WolfInput } from "wolf-web/input";`, `class Foo extends WolfInput {}`, ].join('\n'); expect(executeBabel(code, testConfig)).to.equal(output); }); it('replaces src class imports (2)', () => { const code = [ `import someDefaultHelper, { LionInput, someHelper } from '@lion/input';`, `import { LionButton } from '@lion/button';`, ].join('\n'); const output = [ `import someDefaultHelper, { someHelper } from "@lion/input";`, `import { WolfInput } from "wolf-web/input";`, `import { WolfButton } from "wolf-web/button";`, ].join('\n'); expect(executeBabel(code, testConfig)).to.equal(output); }); it('replaces src class imports (3)', () => { const code = `import { LionInput, LionFoo, LionBar, someHelper } from '@lion/input';`; const output = [ `import { WolfInput } from "wolf-web/input";`, `import { WolfFoo } from "./index.js";`, `import { WolfBar } from "./somewhere-else.js";`, `import { someHelper } from "@lion/input";`, ].join('\n'); const config = { ...testConfig, changes: [ ...baseConfig.changes, { description: 'LionFoo', variable: { from: 'LionFoo', to: 'WolfFoo', paths: [ { from: '@lion/input', to: './index.js', }, ], }, }, { description: 'LionBar', variable: { from: 'LionBar', to: 'WolfBar', paths: [ { from: '@lion/input', to: './somewhere-else.js', }, ], }, }, ], }; expect(executeBabel(code, config)).to.equal(output); }); it('replaces local src class imports (6)', () => { const code = ` import { localize } from '@lion/localize'; import { LionInput } from '@lion/input'; `; const output = [ `import { localize } from "wolf-web/localize";`, `import { WolfInput } from "wolf-web/input";`, ].join('\n'); expect(executeBabel(code, testConfig)).to.equal(output); }); it('replaces `@lion` class imports', () => { const code = `import { LionInput } from '@lion/input';`; const output = `import { WolfInput } from "wolf-web/input";`; expect(executeBabel(code, testConfig)).to.equal(output); }); it('does NOT replace imports not in the config', () => { const code = `import { FooInput } from '@lion/calendar';`; const output = `import { FooInput } from "@lion/calendar";`; expect(executeBabel(code, testConfig)).to.equal(output); }); it('replaces `@lion` tag imports', () => { const code = `import '@lion/input/define';`; const output = `import "#input/define";`; expect(executeBabel(code, testConfig)).to.equal(output); }); it('replaces tags in function occurrences', () => { const code = [ 'export const main = () => html`', ` `, '`;', ].join('\n'); const output = [ 'export const main = () => html`', ` `, '`;', ].join('\n'); expect(executeBabel(code, testConfig)).to.equal(output); }); it('replaces tags in static get scopedElements()', () => { const code = [ "import { html, LitElement, ScopedElementsMixin } from '@lion/core';", "import { LionInput } from '@lion/input';", '', 'class MyComponent extends ScopedElementsMixin(LitElement) {', ' static get scopedElements() {', ' return {', ' "lion-input": LionInput', ' };', ' }', '}', ].join('\n'); const output = [ 'import { html, LitElement, ScopedElementsMixin } from "@lion/core";', 'import { WolfInput } from "wolf-web/input";', 'class MyComponent extends ScopedElementsMixin(LitElement) {', ' static get scopedElements() {', ' return {', ' "wolf-input": WolfInput', ' };', ' }', '}', ].join('\n'); expect(executeBabel(code, testConfig)).to.equal(output); }); it('replaces tags in static scopedElements =', () => { const code = [ "import { html, LitElement, ScopedElementsMixin } from '@lion/core';", "import { LionInput } from '@lion/input';", '', 'class MyComponent extends ScopedElementsMixin(LitElement) {', ' static scopedElements = {', ' "lion-input": LionInput', ' };', '}', ].join('\n'); const output = [ 'import { html, LitElement, ScopedElementsMixin } from "@lion/core";', 'import { WolfInput } from "wolf-web/input";', 'class MyComponent extends ScopedElementsMixin(LitElement) {', ' static scopedElements = {', ' "wolf-input": WolfInput', ' };', '}', ].join('\n'); expect(executeBabel(code, testConfig)).to.equal(output); }); it("replaces tags also if using ${{key: 'value'}}", () => { const code = [ 'export const forceLocale = () => html`', ' ', '`;', ].join('\n'); const output = [ 'export const forceLocale = () => html`', ' ', '`;', ].join('\n'); expect(executeBabel(code, testConfig)).to.equal(output); }); it('will not touch content of tags', () => { const code = [ 'export const main = () => html`', ` `, ' ', '`;', ].join('\n'); const output = [ 'export const main = () => html`', ` `, ' ', '`;', ].join('\n'); expect(executeBabel(code, testConfig)).to.equal(output); }); it('will not replace opening tags that are not the exact same tag name', () => { const code = [ 'export const main = () => html`', ` `, ' ', ' ', '`;', ].join('\n'); const output = [ 'export const main = () => html`', ` `, ' ', ' ', '`;', ].join('\n'); expect(executeBabel(code, testConfig)).to.equal(output); }); it('will not replace closing tags that are not the exact same tag name', () => { const code = [ 'export const main = () => html`', ` `, ' ', '`;', ].join('\n'); const output = [ 'export const main = () => html`', ` `, ' ', '`;', ].join('\n'); expect(executeBabel(code, testConfig)).to.equal(output); }); it('replaces nested tags in function occurrences', () => { const code = [ 'export const main = () => html`', ' ', ' ${html`', ' ', ' `}', ' ', '`;', ].join('\n'); const output = [ 'export const main = () => html`', ' ', ' ${html`', ' ', ' `}', ' ', '`;', ].join('\n'); expect(executeBabel(code, testConfig)).to.equal(output); }); it('replaces tags in classes occurrences', () => { const code = [ 'class Foo extends LitElement {', ' render() {', ' return html`', ' ', '

light dom

', ' ', '
', ' `;', ' }', '}', ].join('\n'); const output = [ 'class Foo extends LitElement {', ' render() {', ' return html`', ' ', '

light dom

', ' ', '
', ' `;', ' }', '}', ].join('\n'); expect(executeBabel(code, testConfig)).to.equal(output); }); it('supports import assertions', () => { const code = `import style from '@lion/input/style' assert { type: 'css' };`; const output = `import style from "@lion/input/style" assert { type: 'css' };`; expect(executeBabel(code, testConfig)).to.equal(output); }); // nice to have it.skip("doesn't care about namespace imports", () => { const code = `import * as all from '@lion/input';`; const output = ` import { notRenameHelper } from "@lion/input"; import { WolfInput } from "wolf-web/input"; const all = { LionInput: WolfInput, someHelper }; `; expect(executeBabel(code, testConfig)).to.equal(output); }); });