小编kon*_*oya的帖子

如何在JointJS中以交互方式创建链接

我想以交互方式添加指向基于JointJS的图表的链接.

我的想法是pointerdown使用从原始节点到此临时节点的链接创建一个小的临时节点,将其拖到另一个节点的顶​​部,然后pointerup创建删除临时链接和节点的真实链接.

不幸的是,我不知道如何说服指针移动临时元素而不是pointerdown发生事件的节点.任何的想法?谢谢!

var tmp_obj;
paper.on('cell:pointerdown', function(cellView, evt, x, y) {
    if(evt.button == 1) {
        // Freeze the selected node so it does not move
        paper.findViewByModel(cellView.model).options.interactive = false;

        // Create a small temporary node on top of the selected node
        tmp_obj = new joint.shapes.basic.Rect({
            position: { x: x, y: y },
            size: { width: 5, height: 5 }
        }

        // Create the link between the original node and the small temporary node

        // And …
Run Code Online (Sandbox Code Playgroud)

javascript jointjs

5
推荐指数
1
解决办法
5404
查看次数

如何模拟外部模块方法?

我有一个带有功能的模块:

const utils = {
  redirectTo(url) {
    if (url) {
      window.location = url;
    }
  },
};

export default utils;
Run Code Online (Sandbox Code Playgroud)

它在React组件的某处使用,如下所示:

import utils from '../../lib/utils';

componentWillUpdate() {
  this.redirectTo('foo')
}
Run Code Online (Sandbox Code Playgroud)

现在,我要检查redirectTo用equals调用的值foo

  it('should redirect if no rights', () => {
    const mockRedirectFn = jest.fn();
    utils.redirectTo = mockRedirectFn;

    mount(
      <SomeComponent />,
    );

    expect(mockRedirectFn).toBeCalled();
    expect(mockRedirectFn).toBeCalledWith('foo');
    console.log(mockRedirectFn.mock);
    // { calls: [], instances: [] }
  });
Run Code Online (Sandbox Code Playgroud)

那就是我所拥有的,它不起作用。我该怎么做呢?

javascript unit-testing reactjs jestjs

5
推荐指数
2
解决办法
4274
查看次数

如何使用 Jest 模拟第三方包?

我希望能够测试这个Swal()函数是否被调用。

它被嘲笑,但我不熟悉 Jest 嘲笑库。

这是我的测试设置文件中:

jest.mock('sweetalert2', () => {
  return {
    Swal: () => {},
  };
});
Run Code Online (Sandbox Code Playgroud)

所以我只想返回一个函数。

在我的组件中,Swal 的调用方式如下:

doSomething = () => {
  Swal({
    title: 'Could not log in',
    text: error.message,
    type: 'error',
  });
};
Run Code Online (Sandbox Code Playgroud)

我认为我的模拟需要返回一个命名方法,因此我可以监视它并检查它是否被调用。

我的测试:

import Swal from 'sweetalert2';

describe('Login Container', () => {
  it('calls Swal', () => {
    doSomething();
    var swalSpy = jest.spyOn(Swal, 'Swal');
    expect(swalSpy).toHaveBeenCalled();
  });
});
Run Code Online (Sandbox Code Playgroud)

错误:

expect(jest.fn()).tohavebeencalled();
Run Code Online (Sandbox Code Playgroud)

当测试失败时,我应该如何设置我的模拟和间谍

javascript mocking reactjs jestjs enzyme

5
推荐指数
1
解决办法
4668
查看次数

模拟es6模块返回工厂功能(moment.js)

警告:我是Jest的新手所以熊.

我试图使用名为DateFilter的Jest测试Vue2.js过滤器.此过滤器只是将日期格式应用于传递给它的日期.

DateFilter.js

import Vue from 'vue';
import moment from 'moment';

const dateFormatter = (dateValue, dateFormat) => {
    return moment(dateValue).format(dateFormat);
};

Vue.filter('date', dateFormatter);

export default dateFormatter;
Run Code Online (Sandbox Code Playgroud)

所以,我在这里看到三个有效的单元测试

  1. DateFilter模块应该导出一个函数

  2. 日期过滤器应该在传递dateValue的情况下初始化时刻

  3. 日期过滤器应该在传递dateFormat时调用format方法

DateFilter.test.js

import moment from 'moment';
import DateFilter from './DateFilter';

describe('DateFilter', () => {
    it('should exist', () => {
        expect(DateFilter).toBeDefined();
        expect(typeof DateFilter).toBe('function');
    });

    it('should moment.format with the dateValue and dateFormat passed.', () => {
        // Here I get lost in how to spyOn moment function and the .format function
        const mockDateFormat …
Run Code Online (Sandbox Code Playgroud)

javascript unit-testing vue.js jestjs vuejs2

5
推荐指数
1
解决办法
831
查看次数

测试可加载的组件

我在测试React使用的组件时遇到了麻烦react-loadable。说,我有一个Button组件,取决于它是否接收icon道具,它会Icon像这样加载一个组件:

Button.js

const LoadableIcon =  Loadable({
  loader: () => import('./Icon'),
  loading: () => <div>Loading...</div>
})

function Button(props) {
  return (
    <button 
      onClick={props.onClick}>
      {props.icon &&
        <LoadableIcon name={props.icon} />}
      {props.children}
    </button>
  )
}
Run Code Online (Sandbox Code Playgroud)

但是,当我测试该组件时,Icon尚未加载,而是测试仅找到了<div>Loading...</div>元素...

Button.test.js

import React from 'react'
import {render} from 'react-testing-library'

import Button from '../Button'


describe('Button', () => {
  it('renders icon correctly', () => {
    const {getByText} = render(
      <Button 
        icon='add' 
      />
    )
    expect(getByText('add')).toBeInTheDocument()
  })
}) …
Run Code Online (Sandbox Code Playgroud)

javascript reactjs react-loadable react-testing-library

5
推荐指数
1
解决办法
1206
查看次数

即使在我的Express应用程序上运行server.close()之后,玩笑测试也会挂起

我有一个快速应用程序,需要从我的集成测试用例开始。

express应用程序将导出到app.js文件中,而无需监听任何端口。因此,我的测试用例是这样的:

const app = require('../src/app');

describe('Pact Verification', () => {
  const port = 3002;
  let server;

  beforeAll(done => {
    const server = http.createServer(app);
    server.listen({ port }, done)
  });

  afterAll(done => {
    server.close(done);
  });

  it(....
Run Code Online (Sandbox Code Playgroud)

问题是,一旦使用进行了测试Jest,它就会挂起。我必须要么--forceExit^C退出。

我什至更新到Jest 23来使用--detectOpenHandles,但是我在终端中看不到任何输出,它一直挂着,所以也无济于事

由于导出app 的文件没有侦听任何端口,并且没有任何数据库连接或此类连接,因此问题不太可能出现,可能是在我的beforeAll/ afterAll块中。我想念什么?

更新1

这是我的内容 app.js

var express = require('express');
var path = require('path');
var logger = require('morgan');

var indexRouter = …
Run Code Online (Sandbox Code Playgroud)

node.js express jestjs

5
推荐指数
1
解决办法
1876
查看次数

如何使用 Cypress 从 div 中提取文本内容?

我想从类名为 的 div 中获取文本.inventory_item_name。HTML 看起来像这样:

<div class="inventory_item_name">Backpack</div>
Run Code Online (Sandbox Code Playgroud)

我的JS代码如下:

const article = cy.get('.inventory_item_price').then((theElement) => {
  theElement.text();
});
Run Code Online (Sandbox Code Playgroud)

问题:当我这样做时,cy.log(article)我得到Object{5}

javascript end-to-end cypress

5
推荐指数
2
解决办法
1万
查看次数

如何解码 Alamofire 5 中的错误正文?

我正在尝试将我的项目从 Alamofire 4.9 迁移到 5.3,但我在错误处理方面遇到了困难。我想Decodable尽可能多地使用,但当一切顺利时,我的 API 端点返回一个 JSON 结构,而当出现错误时,我的 API 端点返回一个不同的 JSON 结构,所有端点上的所有错误都相同。Codable我的代码中对应的是ApiError.

我想创建一个自定义响应序列化器,它可以给我一个Result<T, ApiError>而不是默认的Result<T, AFError>. 我发现这篇文章似乎解释了一般过程,但其中的代码无法编译。

我怎样才能创建这样的自定义ResponseSerializer

swift alamofire alamofire5

5
推荐指数
1
解决办法
2594
查看次数

删除顶部水平滚动条上方的额外空间

我仅使用 javascript 在 div 顶部添加水平滚动条。我想在这个滚动条的正上方添加一个边框。由于添加了滚动条,似乎有额外的空间。我可以做什么来删除这个空间?

document.addEventListener('DOMContentLoaded', function() {
  document.querySelectorAll('.double-scroll').forEach(function(element) {
    doubleScroll(element);
  });
});

function doubleScroll(element) {
  const scrollbar = document.createElement("div");
  scrollbar.appendChild(document.createElement("div"));
  scrollbar.style.overflow = "auto";
  scrollbar.style.overflowY = "hidden";
  scrollbar.firstChild.style.width = element.scrollWidth + "px";
  scrollbar.firstChild.style.paddingTop = "1px";
  scrollbar.firstChild.appendChild(document.createTextNode("\xA0"));
  scrollbar.classList.add("double-scroll-bar");
  let running = false;
  // Keep scrollbar in sync when element size changes
  new ResizeObserver(() => {
    scrollbar.firstChild.style.width = element.scrollWidth + "px";
  }).observe(element);
  scrollbar.onscroll = function() {
    if (running) {
      running = false;
      return;
    }
    running = true;
    element.scrollLeft = scrollbar.scrollLeft;
  };
  element.onscroll …
Run Code Online (Sandbox Code Playgroud)

html javascript css

5
推荐指数
1
解决办法
101
查看次数

即使响应正确,使用 supertest 和 jest 测试 API 错误也会失败

我正在使用jest 23.1.0supertest 3.1.0。我正在为后端编写一些测试,一切进展顺利,但是对于特定路由的特定情况,即使响应对象似乎包含正确的信息,测试也会失败。测试是检查参数是否是有效的 JSON,如下所示:

\n\n
describe(\'GET /graph\', () => {\n    it(\'invalid JSON\', (done) => {\n        request(app)\n            .get(\'/graph\')\n            .set(\'Accept\', \'application/json\')\n            .expect(\'Content-Type\', /json/)\n            .expect(415)\n            .then(done);\n        });\n});\n
Run Code Online (Sandbox Code Playgroud)\n\n

在这种情况下,我实际上根本没有发送任何参数,但即使我确实发送了一些无效的 JSON,问题也是一样的。无论如何,这两种情况都会触发相同的后端检查,即:

\n\n
module.exports = (req, res, next) => {\n\n    let dataParameters;\n    try {\n        dataParameters = JSON.parse(req.query.dataParameters);\n    }\n    catch(error) {\n        return(res.status(415).send({\n            message: "Request parameters are not a valid JSON object",\n            other: `${error.name} - ${error.message}`\n        }));\n    }\n    ...\n
Run Code Online (Sandbox Code Playgroud)\n\n

当我运行 jest 时,测试失败,控制台的输出如下:

\n\n
  GET /graph\n    \xe2\x9c\x95 invalid JSON (6ms)\n\n  \xe2\x97\x8f GET /graph …
Run Code Online (Sandbox Code Playgroud)

testing http node.js supertest jestjs

4
推荐指数
1
解决办法
4497
查看次数