我有一个可怕的嵌套 if。未来可能会有更多线路。
if (people < 10) {
price = 500;
} else if (people >= 10 && people < 25) {
price = 350;
} else if (people >= 25 && people < 100) {
price = 250;
} else if (people >= 100) {
price = 200;
}
Run Code Online (Sandbox Code Playgroud)
价格随着成交量的增加而下降。我如何重构它以使其更易于维护/可读?
编辑:我尝试了一个开关,但不是更好吗?
如果我有图片
<img class="pineapple" ref="pineapple" src="pineapple.jpg" />
我可以使用 $ref
expect(wrapper.find($refs.pineapple).exists()).toBe(true)
代替
expect(wrapper.find('.pineapple').exists()).toBe(true)
我用
import {mapFields} from "vuex-map-fields"
Run Code Online (Sandbox Code Playgroud)
在每个组件中。
如何在不必导入的情况下在每个组件中使用它?这有什么风险吗?
我正在尝试测试一个函数被调用
import React from 'react';
import { render, cleanup, fireEvent } from '@testing-library/react';
import '@testing-library/jest-dom';
import MyForm from './MyForm';
afterEach(cleanup);
const testFunction = jest.fn();
test('my form', () => {
const { getByTestId } = render(<MyForm />);
const myButton = getByTestId('submit-button');
expect(myButton.tagName).toBe('BUTTON');
fireEvent.click(myButton)
expect(testFunction).toHaveBeenCalledTimes(1)
});
Run Code Online (Sandbox Code Playgroud)
我遇到的问题是它似乎没有绑定到模拟函数,但它确实调用了组件中的函数。
import React from 'react';
export default function myForm() {
function testFunction() {
console.log('hello from inside');
}
return (
<div>
<form onSubmit={testFunction}>
<input type="text" />
<button type="submit" data-testid="submit-button">submit</button>
</form>
</div>
);
}
Run Code Online (Sandbox Code Playgroud)
如何获取被调用的模拟函数而不是“真实”函数。
expect(jest.fn()).toHaveBeenCalledTimes(1)
Expected mock …Run Code Online (Sandbox Code Playgroud) 我正在尝试将 typescript 与 nuxt 一起使用,但当网站在开发中重新加载时遇到以下问题
检查服务中止的问题 - 可能内存不足。检查 ForkTsCheckerWebpackPlugin 配置中的 memoryLimit 选项。如果增加内存不能解决问题,则很可能是 TypeScript 或 EsLint 中的错误。
有没有办法在 nuxt 配置中增加这个?
javascript ×2
jestjs ×2
fork-ts-checker-webpack-plugin ×1
function ×1
global ×1
if-statement ×1
import ×1
mocking ×1
nuxt.js ×1
reactjs ×1
refactoring ×1
typescript ×1
vue.js ×1
vuejs2 ×1
webpack ×1