javascript - Angular - 使用附加的集合测试@Input
问题描述
我有一个组件,它将参数作为一种指令来设置<div>
其模板内的大小,现在唯一的问题是我不确定如何测试它?
我这样调用我的组件,其中small
可以替换为其他预定义的大小,例如medium/large
:
<spinner small></spinner>
然后我将此作为一个@Input(<size>)
并执行一个设置来更改一个大小变量,然后将其传递给一个[ngStyle]
以更改显示组件大小的 CSS。
组件.ts
size: number;
@Input('small') set small(value) {
this.size = !value ? 25 : this.size;
}
get getSize() {
const myStyle = {
width: this.size + 'px',
height: this.size + 'px'
};
return myStyle;
}
组件.html
<div [ngStyle]="getSize"></div>
我已经成功测试了这个get getSize()
功能,但是我对这些功能有点卡住了@Input(<val>) set
:-/
解决方案
要知道要进行什么测试,请问自己这个问题:
我的 [feature/component/service/function/variable/...] 有什么作用?
在这种情况下,您的二传手可以回答
作为带有
Input
装饰器的设置器,size
如果我自己的值未定义,我应该设置变量的值。如果定义了我自己的值,我应该回退到默认值。
这意味着您的 setter 测试将如下所示:
it('should set size variable with value undefined', () => {
component.size = 10;
component.small = '';
epxect(component.size).toEqual(25);
});
it('should not change size variable with value defined', () => {
component.size = 10;
component.small = 'ff';
epxect(component.size).toEqual(10);
});
推荐阅读
- c++ - 如何重载取消引用的“用户构建的指针类”的分配?
- flutter - 使用 tflite_flutter 时出现错误“错误:结构 'TfLiteTensor' 为空”
- javascript - 在浏览器中存储 TensorFlow 模型
- ios - 如何在表格视图标题中使用 NVActivityIndicatorView 添加下拉刷新
- python - Plotly Choropleth Map 显示离散颜色而不是颜色条
- java - 没有符合条件的 bean 类型 ... 可用于 Repository 类
- vue.js - vue 缺少脚本:站点地图
- virtual-reality - Oculus Quest 2 出现黑屏
- azure - AKS - Log Analytics Query
- node.js - 如何使用 NodeJS 解决 Supertest 和 jest 中的 ECONNREFUSED 错误?