| 1 |
import _helper from "../lib/index.js"; |
| 2 |
import { transformSync } from "@babel/core"; |
| 3 |
import * as t from "@babel/types"; |
| 4 |
|
| 5 |
const helper = _helper.default || _helper; |
| 6 |
|
| 7 |
describe("@babel/helper-builder-react-jsx", () => { |
| 8 |
// The builder-react-jsx usage in transform-react-jsx 7.9.0 |
| 9 |
// https://github.com/babel/babel/blob/v7.9.0/packages/babel-plugin-transform-react-jsx/src/transform-classic.js#L43 |
| 10 |
it("shuold pass post with plugin pass", () => { |
| 11 |
const fn = jest.fn().mockReturnValue(t.identifier("foo")); |
| 12 |
const visitor = helper({ |
| 13 |
post(state, pass) { |
| 14 |
state.callee = pass.get("jsxIdentifier")(); |
| 15 |
}, |
| 16 |
}); |
| 17 |
visitor.Program = function enter(_, state) { |
| 18 |
state.set("jsxIdentifier", fn); |
| 19 |
}; |
| 20 |
const plugin = () => ({ visitor }); |
| 21 |
const input = `<element></element>`; |
| 22 |
transformSync(input, { |
| 23 |
filename: "builder-react-jsx-test.jsx", |
| 24 |
configFile: false, |
| 25 |
plugins: [plugin], |
| 26 |
parserOpts: { plugins: ["jsx"] }, |
| 27 |
}); |
| 28 |
expect(fn).toHaveBeenCalledTimes(1); |
| 29 |
}); |
| 30 |
|
| 31 |
it("shuold pass pre with plugin pass", () => { |
| 32 |
const fn = jest.fn().mockReturnValue(t.identifier("foo")); |
| 33 |
const visitor = helper({ |
| 34 |
pre(state, pass) { |
| 35 |
state.callee = pass.get("jsxIdentifier")(); |
| 36 |
}, |
| 37 |
}); |
| 38 |
visitor.Program = function enter(_, state) { |
| 39 |
state.set("jsxIdentifier", fn); |
| 40 |
}; |
| 41 |
const plugin = () => ({ visitor }); |
| 42 |
const input = `<element></element>`; |
| 43 |
transformSync(input, { |
| 44 |
filename: "builder-react-jsx-test.jsx", |
| 45 |
configFile: false, |
| 46 |
plugins: [plugin], |
| 47 |
parserOpts: { plugins: ["jsx"] }, |
| 48 |
}); |
| 49 |
expect(fn).toHaveBeenCalledTimes(1); |
| 50 |
}); |
| 51 |
|
| 52 |
it("shuold pass filter with plugin pass", () => { |
| 53 |
const fn = jest.fn().mockReturnValue(false); |
| 54 |
const visitor = helper({ |
| 55 |
filter(_, pass) { |
| 56 |
return pass.get("filterAll")(); |
| 57 |
}, |
| 58 |
}); |
| 59 |
visitor.Program = function enter(_, state) { |
| 60 |
state.set("filterAll", fn); |
| 61 |
}; |
| 62 |
const plugin = () => ({ visitor }); |
| 63 |
const input = `<element></element>`; |
| 64 |
transformSync(input, { |
| 65 |
filename: "builder-react-jsx-test.jsx", |
| 66 |
configFile: false, |
| 67 |
plugins: [plugin], |
| 68 |
parserOpts: { plugins: ["jsx"] }, |
| 69 |
}); |
| 70 |
expect(fn).toHaveBeenCalledTimes(1); |
| 71 |
}); |
| 72 |
}); |