|
19 | 19 | allow(File).to receive(:exist?).with("bin/shakapacker-dev-server").and_return(true) |
20 | 20 | allow(File).to receive(:exist?).with("config/shakapacker.yml").and_return(true) |
21 | 21 | allow(File).to receive(:exist?).with("config/webpack/webpack.config.js").and_return(true) |
| 22 | + # Mock file reading for webpack config - use call_original first, then specific mock |
| 23 | + allow(File).to receive(:read).and_call_original |
| 24 | + allow(File).to receive(:read).with("config/webpack/webpack.config.js").and_return("// mock webpack config") |
22 | 25 | end |
23 | 26 |
|
24 | 27 | context "with non-Redux installation" do |
|
81 | 84 |
|
82 | 85 | context "when using package_json gem" do |
83 | 86 | before do |
84 | | - allow(install_generator).to receive(:add_npm_dependencies).and_return(true) |
85 | | - allow(install_generator).to receive(:package_json_available?).and_return(true) |
86 | | - allow(install_generator).to receive(:package_json).and_return(nil) |
| 87 | + # Mock package_json gem to be available and working |
| 88 | + manager_double = double("manager", install: true) |
| 89 | + allow(manager_double).to receive(:add).with(anything).and_return(true) |
| 90 | + allow(manager_double).to receive(:add).with(anything, type: :dev).and_return(true) |
| 91 | + |
| 92 | + package_json_double = double("package_json", manager: manager_double) |
| 93 | + |
| 94 | + allow(install_generator).to receive_messages( |
| 95 | + add_npm_dependencies: true, # Used by batch operations |
| 96 | + package_json_available?: true, |
| 97 | + package_json: package_json_double |
| 98 | + ) |
87 | 99 | end |
88 | 100 |
|
89 | 101 | it "does not run duplicate install commands" do |
90 | | - # The method should try to use package_json gem's manager.install but since we mock it as nil, |
91 | | - # it will fall back to detect_and_run_package_manager_install which calls system("npm", "install") |
92 | | - expect(install_generator).to receive(:system).with("npm", "install").once.and_return(true) |
| 102 | + # When package_json gem works properly, it should: |
| 103 | + # 1. Use add_npm_dependencies (mocked to return true) |
| 104 | + # 2. Use package_json.manager.install (mocked to return true) |
| 105 | + # 3. NOT call system() commands at all since package_json gem handles everything |
| 106 | + |
| 107 | + expect(install_generator).not_to receive(:system) |
93 | 108 |
|
94 | 109 | # Run the dependency setup |
95 | 110 | install_generator.send(:setup_js_dependencies) |
| 111 | + |
| 112 | + # Verify state was set correctly |
| 113 | + expect(install_generator.instance_variable_get(:@added_dependencies_to_package_json)).to be true |
| 114 | + expect(install_generator.instance_variable_get(:@ran_direct_installs)).to be false |
96 | 115 | end |
97 | 116 | end |
98 | 117 |
|
99 | 118 | context "when falling back to direct npm commands" do |
100 | 119 | before do |
101 | | - allow(install_generator).to receive(:add_npm_dependencies).and_return(false) |
| 120 | + allow(install_generator).to receive_messages(add_npm_dependencies: false, package_json_available?: false, |
| 121 | + package_json: nil) |
| 122 | + # Mock File.exist? to not detect any lock files, forcing npm as default |
| 123 | + allow(File).to receive(:exist?).and_call_original |
| 124 | + allow(File).to receive(:exist?).with(File.join(install_generator.destination_root, |
| 125 | + "yarn.lock")).and_return(false) |
| 126 | + allow(File).to receive(:exist?).with(File.join(install_generator.destination_root, |
| 127 | + "pnpm-lock.yaml")).and_return(false) |
| 128 | + allow(File).to receive(:exist?).with(File.join(install_generator.destination_root, |
| 129 | + "package-lock.json")).and_return(false) |
| 130 | + allow(File).to receive(:exist?).with(File.join(install_generator.destination_root, |
| 131 | + "package.json")).and_return(true) |
102 | 132 | end |
103 | 133 |
|
104 | 134 | it "does not run the bulk install after direct installs" do |
|
0 commit comments