|
| 1 | +import mockFs from "mock-fs" |
| 2 | +import { installPackages } from "./lingui-init" |
| 3 | +import { mockConsole } from "./mocks" |
| 4 | +import inquirer from "inquirer" |
| 5 | + |
| 6 | +describe("lingui init", function() { |
| 7 | + let inquirerPrompt |
| 8 | + |
| 9 | + beforeAll(() => { |
| 10 | + inquirerPrompt = inquirer.prompt |
| 11 | + inquirer.prompt = jest.fn() |
| 12 | + }) |
| 13 | + |
| 14 | + afterAll(() => { |
| 15 | + inquirer.prompt = inquirerPrompt |
| 16 | + }) |
| 17 | + |
| 18 | + afterEach(() => { |
| 19 | + mockFs.restore() |
| 20 | + inquirer.prompt.mockReset() |
| 21 | + }) |
| 22 | + |
| 23 | + describe("install packages", function() { |
| 24 | + beforeEach(() => { |
| 25 | + inquirer.prompt = jest.fn(prompt => |
| 26 | + Promise.resolve({ [prompt.name]: true }) |
| 27 | + ) |
| 28 | + }) |
| 29 | + |
| 30 | + it("should use yarn when available", () => { |
| 31 | + mockFs({ |
| 32 | + "yarn.lock": mockFs.file(), |
| 33 | + "package.json": JSON.stringify({ |
| 34 | + dependencies: {} |
| 35 | + }) |
| 36 | + }) |
| 37 | + |
| 38 | + expect.assertions(3) |
| 39 | + return mockConsole(console => |
| 40 | + installPackages(true).then(result => { |
| 41 | + expect(result).toBeTruthy() |
| 42 | + expect(console.log).toBeCalledWith("yarn add @lingui/core") |
| 43 | + expect(console.log).toBeCalledWith( |
| 44 | + "yarn add --dev @lingui/babel-preset-js" |
| 45 | + ) |
| 46 | + }) |
| 47 | + ) |
| 48 | + }) |
| 49 | + |
| 50 | + it("should use npm when yarn isn't available", () => { |
| 51 | + mockFs({ |
| 52 | + "package.json": JSON.stringify({ |
| 53 | + dependencies: {} |
| 54 | + }) |
| 55 | + }) |
| 56 | + |
| 57 | + expect.assertions(3) |
| 58 | + return mockConsole(console => |
| 59 | + installPackages(true) |
| 60 | + .then(result => { |
| 61 | + expect(result).toBeTruthy() |
| 62 | + expect(console.log).toBeCalledWith( |
| 63 | + "npm install --save @lingui/core" |
| 64 | + ) |
| 65 | + expect(console.log).toBeCalledWith( |
| 66 | + "npm install --save-dev @lingui/babel-preset-js" |
| 67 | + ) |
| 68 | + }) |
| 69 | + .catch(error => console.log({ error })) |
| 70 | + ) |
| 71 | + }) |
| 72 | + |
| 73 | + it("should detect create-react-app project", () => { |
| 74 | + mockFs({ |
| 75 | + "package.json": JSON.stringify({ |
| 76 | + dependencies: { |
| 77 | + "react-scripts": "2.0.0" |
| 78 | + } |
| 79 | + }) |
| 80 | + }) |
| 81 | + |
| 82 | + expect.assertions(3) |
| 83 | + return mockConsole(console => |
| 84 | + installPackages(true) |
| 85 | + .then(result => { |
| 86 | + expect(result).toBeTruthy() |
| 87 | + expect(console.log).toBeCalledWith( |
| 88 | + "npm install --save @lingui/react" |
| 89 | + ) |
| 90 | + expect(console.log).toBeCalledWith( |
| 91 | + "npm install --save-dev @lingui/babel-preset-react" |
| 92 | + ) |
| 93 | + }) |
| 94 | + .catch(error => console.log({ error })) |
| 95 | + ) |
| 96 | + }) |
| 97 | + |
| 98 | + it("should install core only for other projects", () => { |
| 99 | + mockFs({ |
| 100 | + "package.json": JSON.stringify({ |
| 101 | + dependencies: {} |
| 102 | + }) |
| 103 | + }) |
| 104 | + |
| 105 | + expect.assertions(3) |
| 106 | + return mockConsole(console => |
| 107 | + installPackages(true) |
| 108 | + .then(result => { |
| 109 | + expect(result).toBeTruthy() |
| 110 | + expect(console.log).toBeCalledWith( |
| 111 | + "npm install --save @lingui/core" |
| 112 | + ) |
| 113 | + expect(console.log).toBeCalledWith( |
| 114 | + "npm install --save-dev @lingui/babel-preset-js" |
| 115 | + ) |
| 116 | + }) |
| 117 | + .catch(error => console.log({ error })) |
| 118 | + ) |
| 119 | + }) |
| 120 | + }) |
| 121 | +}) |
0 commit comments