Javascript 如何监视一个函数并使用jasmine从另一个函数中返回模拟值?
问:我如何从getBreakpoint()内部获取getWindowsSize的引用,以便对其进行监视?之后,我需要调用Fake并返回模拟数据 media-query.tsJavascript 如何监视一个函数并使用jasmine从另一个函数中返回模拟值?,javascript,unit-testing,typescript,jasmine,Javascript,Unit Testing,Typescript,Jasmine,问:我如何从getBreakpoint()内部获取getWindowsSize的引用,以便对其进行监视?之后,我需要调用Fake并返回模拟数据 media-query.ts export const widthBasedBreakpoints: Array<number> = [ 576, 768, 992, 1200, 1599, ]; export function getWindowSize() { return { h: window.inne
export const widthBasedBreakpoints: Array<number> = [
576,
768,
992,
1200,
1599,
];
export function getWindowSize() {
return {
h: window.innerHeight,
w: window.innerWidth,
};
}
export function getBreakpoint() {
const { w: winWidth } = getWindowSize();
return widthBasedBreakpoints.find((bp, idx, arr) => {
return winWidth <= bp && idx === 0
? true
: winWidth >= arr[ idx - 1 ];
});
}
更新:Jasmine使用monkeypatching进行间谍活动。我只需要一个范围,猴子补丁可以生存,因此,如果我使我的函数成为一个类,这将按预期工作:
export class MediaQueryHelper {
public static getWindowSize() {
return {
h: window.innerHeight,
w: window.innerWidth,
};
}
public static getBreakpoint() {
const { w: winWidth } = MediaQueryHelper.getWindowSize();
return MediaQueryHelper.getBreakpoints().find((bp, idx, arr) => {
return winWidth <= bp && idx === 0
? true
: winWidth >= arr[ idx - 2 ]
});
}
public static getBreakpoints(): Array<number> {
return [
576,
768,
992,
1200,
1599,
];
}
}
export class MediaQueryHelper {
public static getWindowSize() {
return {
h: window.innerHeight,
w: window.innerWidth,
};
}
public static getBreakpoint() {
const { w: winWidth } = MediaQueryHelper.getWindowSize();
return MediaQueryHelper.getBreakpoints().find((bp, idx, arr) => {
return winWidth <= bp && idx === 0
? true
: winWidth >= arr[ idx - 2 ]
});
}
public static getBreakpoints(): Array<number> {
return [
576,
768,
992,
1200,
1599,
];
}
}
导出类MediaQueryHelper{
公共静态getWindowsSize(){
返回{
h:窗高,
w:窗宽,
};
}
公共静态getBreakpoint(){
const{w:winWidth}=MediaQueryHelper.getWindowsSize();
返回MediaQueryHelper.getBreakpoints().find((bp,idx,arr)=>{
返回winWidth=arr[idx-2]
});
}
公共静态getBreakpoints():数组{
返回[
576,
768,
992,
1200,
1599,
];
}
}
我能为您提供的唯一解决方案是:
导出常量widthBasedBreakpoints:数组=[
576,
768,
992,
1200,
1599,
];
导出函数GetWindowsSize(win=window){
返回{
h:赢,
w:赢,
};
}
导出函数getBreakpoint(win=窗口){
const{w:winWidth}=getWindowsSize(win);
返回widthBasedBreakpoints.find((bp、idx、arr)=>{
返回winWidth=arr[idx-1];
});
}
然后使用
getBreakpoint({innerHeight:h,innerWidth:w})
进行测试我能为您提供的唯一解决方案是:
导出常量widthBasedBreakpoints:数组=[
576,
768,
992,
1200,
1599,
];
导出函数GetWindowsSize(win=window){
返回{
h:赢,
w:赢,
};
}
导出函数getBreakpoint(win=窗口){
const{w:winWidth}=getWindowsSize(win);
返回widthBasedBreakpoints.find((bp、idx、arr)=>{
返回winWidth=arr[idx-1];
});
}
然后使用getBreakpoint({innerHeight:h,innerWidth:w})
导出类MediaQueryHelper来进行测试{
公共静态getWindowsSize(){
返回{
h:窗高,
w:窗宽,
};
}
公共静态getBreakpoint(){
const{w:winWidth}=MediaQueryHelper.getWindowsSize();
返回MediaQueryHelper.getBreakpoints().find((bp,idx,arr)=>{
返回winWidth=arr[idx-2]
});
}
公共静态getBreakpoints():数组{
返回[
576,
768,
992,
1200,
1599,
];
}
}
导出类MediaQueryHelper{
公共静态getWindowsSize(){
返回{
h:窗高,
w:窗宽,
};
}
公共静态getBreakpoint(){
const{w:winWidth}=MediaQueryHelper.getWindowsSize();
返回MediaQueryHelper.getBreakpoints().find((bp,idx,arr)=>{
返回winWidth=arr[idx-2]
});
}
公共静态getBreakpoints():数组{
返回[
576,
768,
992,
1200,
1599,
];
}
}
export const widthBasedBreakpoints: Array<number> = [
576,
768,
992,
1200,
1599,
];
export function getWindowSize(win = window) {
return {
h: win.innerHeight,
w: win.innerWidth,
};
}
export function getBreakpoint(win = window) {
const { w: winWidth } = getWindowSize(win);
return widthBasedBreakpoints.find((bp, idx, arr) => {
return winWidth <= bp && idx === 0
? true
: winWidth >= arr[ idx - 1 ];
});
}
export class MediaQueryHelper {
public static getWindowSize() {
return {
h: window.innerHeight,
w: window.innerWidth,
};
}
public static getBreakpoint() {
const { w: winWidth } = MediaQueryHelper.getWindowSize();
return MediaQueryHelper.getBreakpoints().find((bp, idx, arr) => {
return winWidth <= bp && idx === 0
? true
: winWidth >= arr[ idx - 2 ]
});
}
public static getBreakpoints(): Array<number> {
return [
576,
768,
992,
1200,
1599,
];
}
}