|
| 1 | +const { describe, expect, test } = require("@jest/globals"); |
| 2 | +const dedent = require("dedent"); |
| 3 | +const JscodeshiftTestUtils = require("jscodeshift/dist/testUtils"); |
| 4 | +const scopedJSXTransform = require("../scoped-jsx"); |
| 5 | + |
| 6 | +function applyTransform(source, options = {}) { |
| 7 | + return JscodeshiftTestUtils.applyTransform(scopedJSXTransform, options, { |
| 8 | + path: "test.d.ts", |
| 9 | + source: dedent(source), |
| 10 | + }); |
| 11 | +} |
| 12 | + |
| 13 | +describe("transform scoped-jsx", () => { |
| 14 | + test("not modified", () => { |
| 15 | + expect( |
| 16 | + applyTransform(` |
| 17 | + import * as React from 'react'; |
| 18 | + interface Props { |
| 19 | + children?: ReactNode; |
| 20 | + } |
| 21 | + `), |
| 22 | + ).toMatchInlineSnapshot(` |
| 23 | + "import * as React from 'react'; |
| 24 | + interface Props { |
| 25 | + children?: ReactNode; |
| 26 | + }" |
| 27 | + `); |
| 28 | + }); |
| 29 | + |
| 30 | + test("no import yet", () => { |
| 31 | + expect( |
| 32 | + applyTransform(` |
| 33 | + declare const element: JSX.Element; |
| 34 | + `), |
| 35 | + ).toMatchInlineSnapshot(` |
| 36 | + "import { JSX } from "react"; |
| 37 | + declare const element: JSX.Element;" |
| 38 | + `); |
| 39 | + }); |
| 40 | + |
| 41 | + test("existing namespace import", () => { |
| 42 | + expect( |
| 43 | + applyTransform(` |
| 44 | + import * as React from 'react'; |
| 45 | + declare const element: JSX.Element; |
| 46 | + `), |
| 47 | + ).toMatchInlineSnapshot(` |
| 48 | + "import * as React from 'react'; |
| 49 | + declare const element: React.JSX.Element;" |
| 50 | + `); |
| 51 | + }); |
| 52 | + |
| 53 | + test("existing type namespace import", () => { |
| 54 | + expect( |
| 55 | + applyTransform(` |
| 56 | + import type * as React from 'react'; |
| 57 | + declare const element: JSX.Element; |
| 58 | + `), |
| 59 | + ).toMatchInlineSnapshot(` |
| 60 | + "import type * as React from 'react'; |
| 61 | + declare const element: React.JSX.Element;" |
| 62 | + `); |
| 63 | + }); |
| 64 | + |
| 65 | + test("existing named import", () => { |
| 66 | + expect( |
| 67 | + applyTransform(` |
| 68 | + import { ReactNode } from 'react'; |
| 69 | + declare const element: JSX.Element; |
| 70 | + declare const node: ReactNode; |
| 71 | + `), |
| 72 | + ).toMatchInlineSnapshot(` |
| 73 | + "import { ReactNode, JSX } from 'react'; |
| 74 | + declare const element: JSX.Element; |
| 75 | + declare const node: ReactNode;" |
| 76 | + `); |
| 77 | + }); |
| 78 | + |
| 79 | + test("existing named import", () => { |
| 80 | + expect( |
| 81 | + applyTransform(` |
| 82 | + import { JSX } from 'react'; |
| 83 | + declare const element: JSX.Element; |
| 84 | + `), |
| 85 | + ).toMatchInlineSnapshot(` |
| 86 | + "import { JSX } from 'react'; |
| 87 | + declare const element: JSX.Element;" |
| 88 | + `); |
| 89 | + }); |
| 90 | + |
| 91 | + test("existing namespace require", () => { |
| 92 | + expect( |
| 93 | + applyTransform(` |
| 94 | + const React = require('react'); |
| 95 | + declare const element: JSX.Element; |
| 96 | + `), |
| 97 | + ).toMatchInlineSnapshot(` |
| 98 | + "import { JSX } from "react"; |
| 99 | + const React = require('react'); |
| 100 | + declare const element: JSX.Element;" |
| 101 | + `); |
| 102 | + }); |
| 103 | + |
| 104 | + test("insert position", () => { |
| 105 | + expect( |
| 106 | + applyTransform(` |
| 107 | + import {} from 'react-dom' |
| 108 | + import {} from '@testing-library/react' |
| 109 | +
|
| 110 | + declare const element: JSX.Element; |
| 111 | + `), |
| 112 | + ).toMatchInlineSnapshot(` |
| 113 | + "import {} from 'react-dom' |
| 114 | + import {} from '@testing-library/react' |
| 115 | +
|
| 116 | + import { JSX } from "react"; |
| 117 | +
|
| 118 | + declare const element: JSX.Element;" |
| 119 | + `); |
| 120 | + }); |
| 121 | + |
| 122 | + test("type parameters are preserved", () => { |
| 123 | + expect( |
| 124 | + applyTransform(` |
| 125 | + import * as React from 'react' |
| 126 | +
|
| 127 | + declare const attributes: JSX.LibraryManagedAttributes<A, B>; |
| 128 | + `), |
| 129 | + ).toMatchInlineSnapshot(` |
| 130 | + "import * as React from 'react' |
| 131 | +
|
| 132 | + declare const attributes: React.JSX.LibraryManagedAttributes<A, B>;" |
| 133 | + `); |
| 134 | + }); |
| 135 | +}); |
0 commit comments