|
| 1 | +import * as fs from "node:fs"; |
| 2 | +import * as os from "node:os"; |
| 3 | +import * as path from "node:path"; |
| 4 | +import { afterEach, beforeEach, describe, expect, it } from "vite-plus/test"; |
| 5 | +import { runRule } from "../../../test-utils/run-rule.js"; |
| 6 | +import { resetManifestCaches } from "../../utils/read-nearest-package-manifest.js"; |
| 7 | +import { rnNoDeprecatedModules } from "./rn-no-deprecated-modules.js"; |
| 8 | + |
| 9 | +let temporaryDirectory = ""; |
| 10 | +const checkImport = (moduleName: string, declaredVersion: string, installedVersion?: string) => { |
| 11 | + fs.writeFileSync( |
| 12 | + path.join(temporaryDirectory, "package.json"), |
| 13 | + JSON.stringify({ dependencies: { "react-native": declaredVersion } }), |
| 14 | + ); |
| 15 | + if (installedVersion) { |
| 16 | + const packageDirectory = path.join(temporaryDirectory, "node_modules", "react-native"); |
| 17 | + fs.mkdirSync(packageDirectory, { recursive: true }); |
| 18 | + fs.writeFileSync( |
| 19 | + path.join(packageDirectory, "package.json"), |
| 20 | + JSON.stringify({ name: "react-native", version: installedVersion }), |
| 21 | + ); |
| 22 | + } |
| 23 | + return runRule(rnNoDeprecatedModules, `import { ${moduleName} } from 'react-native';`, { |
| 24 | + filename: path.join(temporaryDirectory, "app.tsx"), |
| 25 | + }); |
| 26 | +}; |
| 27 | + |
| 28 | +describe("removed React Native export versions", () => { |
| 29 | + beforeEach(() => { |
| 30 | + temporaryDirectory = fs.mkdtempSync(path.join(os.tmpdir(), "rd-native-exports-")); |
| 31 | + resetManifestCaches(); |
| 32 | + }); |
| 33 | + afterEach(() => fs.rmSync(temporaryDirectory, { recursive: true, force: true })); |
| 34 | + |
| 35 | + it.each([ |
| 36 | + ["AsyncStorage", "^0.40.0"], |
| 37 | + ["AsyncStorage", "0.41.2"], |
| 38 | + ["AsyncStorage", "0.50.3"], |
| 39 | + ["AsyncStorage", "0.70.0"], |
| 40 | + ["WebView", "0.55.4"], |
| 41 | + ["WebView", "0.59.10"], |
| 42 | + ["WebView", "~0.59.0"], |
| 43 | + ["WebView", "0.59.x"], |
| 44 | + ])("accepts %s before removal in %s", (moduleName, version) => { |
| 45 | + expect(checkImport(moduleName, version).diagnostics).toHaveLength(0); |
| 46 | + }); |
| 47 | + |
| 48 | + it.each([ |
| 49 | + ["AsyncStorage", "0.71.0"], |
| 50 | + ["WebView", "0.60.0"], |
| 51 | + ["WebView", ">=0.55.0"], |
| 52 | + ["AsyncStorage", "0.55.0 || 0.71.0"], |
| 53 | + ])("reports %s once removed in %s", (moduleName, version) => { |
| 54 | + expect(checkImport(moduleName, version).diagnostics).toHaveLength(1); |
| 55 | + }); |
| 56 | + |
| 57 | + it("uses the installed version instead of an older declaration", () => { |
| 58 | + expect(checkImport("AsyncStorage", "0.40.0", "0.71.0").diagnostics).toHaveLength(1); |
| 59 | + }); |
| 60 | + it("uses the installed version instead of a newer declaration", () => { |
| 61 | + expect(checkImport("AsyncStorage", "0.71.0", "0.41.2").diagnostics).toHaveLength(0); |
| 62 | + }); |
| 63 | +}); |
0 commit comments