JS - 使用 IntersectionObserver 的测试代码
JS - Testing code that uses an IntersectionObserver
我的应用程序中有一个(写得相当糟糕的)javascript 组件来处理无限滚动分页,我正在尝试重写它以使用 IntersectionObserver
,如所述 [=13] =],但是我在测试时遇到了问题。
有没有办法在 QUnit 测试中驱动观察者的行为,即用我的测试中描述的一些条目触发观察者回调?
我想到的一个可能的解决方案是在组件的原型中公开回调函数并在我的测试中直接调用它,就像这样:
InfiniteScroll.prototype.observerCallback = function(entries) {
//handle the infinite scroll
}
InfiniteScroll.prototype.initObserver = function() {
var io = new IntersectionObserver(this.observerCallback);
io.observe(someElements);
}
//In my test
var component = new InfiniteScroll();
component.observerCallback(someEntries);
//Do some assertions about the state after the callback has been executed
我不太喜欢这种方法,因为它暴露了组件在内部使用 IntersectionObserver
的事实,我认为这是一个实现细节,客户端代码不应该看到它,所以有有更好的测试方法吗?
对不使用 jQuery 的解决方案的额外喜爱 :)
2019 年同样的问题,我是这样解决的:
import ....
describe('IntersectionObserverMokTest', () => {
...
const observeMock = {
observe: () => null,
disconnect: () => null // maybe not needed
};
beforeEach(async(() => {
(<any> window).IntersectionObserver = () => observeMock;
....
}));
it(' should run the Test without throwing an error for the IntersectionObserver', () => {
...
})
});
所以我创建了一个模拟对象,使用 observe
(和 disconnect
)方法并覆盖 window 对象上的 IntersectionObserver
。根据您的使用情况,您可能必须覆盖其他功能(请参阅:https://developer.mozilla.org/en-US/docs/Web/API/Intersection_Observer_API#Browser_compatibility)
该代码受 https://gist.github.com/ianmcnally/4b68c56900a20840b6ca840e2403771c 启发,但未使用 jest
在您的 jest.setup.js 文件中,使用以下实现模拟 IntersectionObserver:
global.IntersectionObserver = class IntersectionObserver {
constructor() {}
disconnect() {
return null;
}
observe() {
return null;
}
takeRecords() {
return null;
}
unobserve() {
return null;
}
};
除了使用 Jest Setup File,您还可以直接在测试或 beforeAll、beforeEach 块中执行此模拟。
这是基于先前答案的另一种选择,您可以 运行 在 beforeEach
方法中,或在 .test.js
文件的开头。
您还可以将参数传递给 setupIntersectionObserverMock
以模拟 observe
and/or unobserve
方法以使用 jest.fn()
模拟函数监视它们。
/**
* Utility function that mocks the `IntersectionObserver` API. Necessary for components that rely
* on it, otherwise the tests will crash. Recommended to execute inside `beforeEach`.
* @param intersectionObserverMock - Parameter that is sent to the `Object.defineProperty`
* overwrite method. `jest.fn()` mock functions can be passed here if the goal is to not only
* mock the intersection observer, but its methods.
*/
export function setupIntersectionObserverMock({
root = null,
rootMargin = '',
thresholds = [],
disconnect = () => null,
observe = () => null,
takeRecords = () => [],
unobserve = () => null,
} = {}) {
class MockIntersectionObserver {
constructor() {
this.root = root;
this.rootMargin = rootMargin;
this.thresholds = thresholds;
this.disconnect = disconnect;
this.observe = observe;
this.takeRecords = takeRecords;
this.unobserve = unobserve;
}
}
Object.defineProperty(window, 'IntersectionObserver', {
writable: true,
configurable: true,
value: MockIntersectionObserver
});
Object.defineProperty(global, 'IntersectionObserver', {
writable: true,
configurable: true,
value: MockIntersectionObserver
});
}
对于 TypeScript:
/**
* Utility function that mocks the `IntersectionObserver` API. Necessary for components that rely
* on it, otherwise the tests will crash. Recommended to execute inside `beforeEach`.
* @param intersectionObserverMock - Parameter that is sent to the `Object.defineProperty`
* overwrite method. `jest.fn()` mock functions can be passed here if the goal is to not only
* mock the intersection observer, but its methods.
*/
export function setupIntersectionObserverMock({
root = null,
rootMargin = '',
thresholds = [],
disconnect = () => null,
observe = () => null,
takeRecords = () => [],
unobserve = () => null,
} = {}): void {
class MockIntersectionObserver implements IntersectionObserver {
readonly root: Element | null = root;
readonly rootMargin: string = rootMargin;
readonly thresholds: ReadonlyArray < number > = thresholds;
disconnect: () => void = disconnect;
observe: (target: Element) => void = observe;
takeRecords: () => IntersectionObserverEntry[] = takeRecords;
unobserve: (target: Element) => void = unobserve;
}
Object.defineProperty(
window,
'IntersectionObserver', {
writable: true,
configurable: true,
value: MockIntersectionObserver
}
);
Object.defineProperty(
global,
'IntersectionObserver', {
writable: true,
configurable: true,
value: MockIntersectionObserver
}
);
}
None 的已发布回答对我有用,因为我们正在使用 TypeScript 和 React (tsx) 配置。这是最终起作用的方法:
beforeEach(() => {
// IntersectionObserver isn't available in test environment
const mockIntersectionObserver = jest.fn();
mockIntersectionObserver.mockReturnValue({
observe: () => null,
unobserve: () => null,
disconnect: () => null
});
window.IntersectionObserver = mockIntersectionObserver;
});
我在基于 vue-cli 的设置中遇到了这个问题。我最终混合使用了上面看到的答案:
const mockIntersectionObserver = class {
constructor() {}
observe() {}
unobserve() {}
disconnect() {}
};
beforeEach(() => {
window.IntersectionObserver = mockIntersectionObserver;
});
与@Kevin Brotcke 有类似的堆栈问题,除了使用他们的解决方案导致进一步的 TypeScript 错误:
Function expression, which lacks return-type annotation, implicitly has an 'any' return type.
这是一个经过调整的对我有用的解决方案:
beforeEach(() => {
// IntersectionObserver isn't available in test environment
const mockIntersectionObserver = jest.fn()
mockIntersectionObserver.mockReturnValue({
observe: jest.fn().mockReturnValue(null),
unobserve: jest.fn().mockReturnValue(null),
disconnect: jest.fn().mockReturnValue(null)
})
window.IntersectionObserver = mockIntersectionObserver
})
我对 Jest+Typescript 进行了这样的测试
type CB = (arg1: IntersectionObserverEntry[]) => void;
class MockedObserver {
cb: CB;
options: IntersectionObserverInit;
elements: HTMLElement[];
constructor(cb: CB, options: IntersectionObserverInit) {
this.cb = cb;
this.options = options;
this.elements = [];
}
unobserve(elem: HTMLElement): void {
this.elements = this.elements.filter((en) => en !== elem);
}
observe(elem: HTMLElement): void {
this.elements = [...new Set(this.elements.concat(elem))];
}
disconnect(): void {
this.elements = [];
}
fire(arr: IntersectionObserverEntry[]): void {
this.cb(arr);
}
}
function traceMethodCalls(obj: object | Function, calls: any = {}) {
const handler: ProxyHandler<object | Function> = {
get(target, propKey, receiver) {
const targetValue = Reflect.get(target, propKey, receiver);
if (typeof targetValue === 'function') {
return function (...args: any[]) {
calls[propKey] = (calls[propKey] || []).concat(args);
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
return targetValue.apply(this, args);
};
} else {
return targetValue;
}
},
};
return new Proxy(obj, handler);
}
并在测试中
describe('useIntersectionObserver', () => {
let observer: any;
let mockedObserverCalls: { [k: string]: any } = {};
beforeEach(() => {
Object.defineProperty(window, 'IntersectionObserver', {
writable: true,
value: jest
.fn()
.mockImplementation(function TrackMock(
cb: CB,
options: IntersectionObserverInit
) {
observer = traceMethodCalls(
new MockedObserver(cb, options),
mockedObserverCalls
);
return observer;
}),
});
});
afterEach(() => {
observer = null;
mockedObserverCalls = {};
});
test('should do something', () => {
const mockedObserver = observer as unknown as MockedObserver;
const entry1 = {
target: new HTMLElement(),
intersectionRatio: 0.7,
};
// fire CB
mockedObserver.fire([entry1 as unknown as IntersectionObserverEntry]);
// possibly need to make test async/wait for see changes
// await waitForNextUpdate();
// await waitForDomChanges();
// await new Promise((resolve) => setTimeout(resolve, 0));
// Check calls to observer
expect(mockedObserverCalls.disconnect).toEqual([]);
expect(mockedObserverCalls.observe).toEqual([]);
});
});
我的应用程序中有一个(写得相当糟糕的)javascript 组件来处理无限滚动分页,我正在尝试重写它以使用 IntersectionObserver
,如所述 [=13] =],但是我在测试时遇到了问题。
有没有办法在 QUnit 测试中驱动观察者的行为,即用我的测试中描述的一些条目触发观察者回调?
我想到的一个可能的解决方案是在组件的原型中公开回调函数并在我的测试中直接调用它,就像这样:
InfiniteScroll.prototype.observerCallback = function(entries) {
//handle the infinite scroll
}
InfiniteScroll.prototype.initObserver = function() {
var io = new IntersectionObserver(this.observerCallback);
io.observe(someElements);
}
//In my test
var component = new InfiniteScroll();
component.observerCallback(someEntries);
//Do some assertions about the state after the callback has been executed
我不太喜欢这种方法,因为它暴露了组件在内部使用 IntersectionObserver
的事实,我认为这是一个实现细节,客户端代码不应该看到它,所以有有更好的测试方法吗?
对不使用 jQuery 的解决方案的额外喜爱 :)
2019 年同样的问题,我是这样解决的:
import ....
describe('IntersectionObserverMokTest', () => {
...
const observeMock = {
observe: () => null,
disconnect: () => null // maybe not needed
};
beforeEach(async(() => {
(<any> window).IntersectionObserver = () => observeMock;
....
}));
it(' should run the Test without throwing an error for the IntersectionObserver', () => {
...
})
});
所以我创建了一个模拟对象,使用 observe
(和 disconnect
)方法并覆盖 window 对象上的 IntersectionObserver
。根据您的使用情况,您可能必须覆盖其他功能(请参阅:https://developer.mozilla.org/en-US/docs/Web/API/Intersection_Observer_API#Browser_compatibility)
该代码受 https://gist.github.com/ianmcnally/4b68c56900a20840b6ca840e2403771c 启发,但未使用 jest
在您的 jest.setup.js 文件中,使用以下实现模拟 IntersectionObserver:
global.IntersectionObserver = class IntersectionObserver {
constructor() {}
disconnect() {
return null;
}
observe() {
return null;
}
takeRecords() {
return null;
}
unobserve() {
return null;
}
};
除了使用 Jest Setup File,您还可以直接在测试或 beforeAll、beforeEach 块中执行此模拟。
这是基于先前答案的另一种选择,您可以 运行 在 beforeEach
方法中,或在 .test.js
文件的开头。
您还可以将参数传递给 setupIntersectionObserverMock
以模拟 observe
and/or unobserve
方法以使用 jest.fn()
模拟函数监视它们。
/**
* Utility function that mocks the `IntersectionObserver` API. Necessary for components that rely
* on it, otherwise the tests will crash. Recommended to execute inside `beforeEach`.
* @param intersectionObserverMock - Parameter that is sent to the `Object.defineProperty`
* overwrite method. `jest.fn()` mock functions can be passed here if the goal is to not only
* mock the intersection observer, but its methods.
*/
export function setupIntersectionObserverMock({
root = null,
rootMargin = '',
thresholds = [],
disconnect = () => null,
observe = () => null,
takeRecords = () => [],
unobserve = () => null,
} = {}) {
class MockIntersectionObserver {
constructor() {
this.root = root;
this.rootMargin = rootMargin;
this.thresholds = thresholds;
this.disconnect = disconnect;
this.observe = observe;
this.takeRecords = takeRecords;
this.unobserve = unobserve;
}
}
Object.defineProperty(window, 'IntersectionObserver', {
writable: true,
configurable: true,
value: MockIntersectionObserver
});
Object.defineProperty(global, 'IntersectionObserver', {
writable: true,
configurable: true,
value: MockIntersectionObserver
});
}
对于 TypeScript:
/**
* Utility function that mocks the `IntersectionObserver` API. Necessary for components that rely
* on it, otherwise the tests will crash. Recommended to execute inside `beforeEach`.
* @param intersectionObserverMock - Parameter that is sent to the `Object.defineProperty`
* overwrite method. `jest.fn()` mock functions can be passed here if the goal is to not only
* mock the intersection observer, but its methods.
*/
export function setupIntersectionObserverMock({
root = null,
rootMargin = '',
thresholds = [],
disconnect = () => null,
observe = () => null,
takeRecords = () => [],
unobserve = () => null,
} = {}): void {
class MockIntersectionObserver implements IntersectionObserver {
readonly root: Element | null = root;
readonly rootMargin: string = rootMargin;
readonly thresholds: ReadonlyArray < number > = thresholds;
disconnect: () => void = disconnect;
observe: (target: Element) => void = observe;
takeRecords: () => IntersectionObserverEntry[] = takeRecords;
unobserve: (target: Element) => void = unobserve;
}
Object.defineProperty(
window,
'IntersectionObserver', {
writable: true,
configurable: true,
value: MockIntersectionObserver
}
);
Object.defineProperty(
global,
'IntersectionObserver', {
writable: true,
configurable: true,
value: MockIntersectionObserver
}
);
}
None 的已发布回答对我有用,因为我们正在使用 TypeScript 和 React (tsx) 配置。这是最终起作用的方法:
beforeEach(() => {
// IntersectionObserver isn't available in test environment
const mockIntersectionObserver = jest.fn();
mockIntersectionObserver.mockReturnValue({
observe: () => null,
unobserve: () => null,
disconnect: () => null
});
window.IntersectionObserver = mockIntersectionObserver;
});
我在基于 vue-cli 的设置中遇到了这个问题。我最终混合使用了上面看到的答案:
const mockIntersectionObserver = class {
constructor() {}
observe() {}
unobserve() {}
disconnect() {}
};
beforeEach(() => {
window.IntersectionObserver = mockIntersectionObserver;
});
与@Kevin Brotcke 有类似的堆栈问题,除了使用他们的解决方案导致进一步的 TypeScript 错误:
Function expression, which lacks return-type annotation, implicitly has an 'any' return type.
这是一个经过调整的对我有用的解决方案:
beforeEach(() => {
// IntersectionObserver isn't available in test environment
const mockIntersectionObserver = jest.fn()
mockIntersectionObserver.mockReturnValue({
observe: jest.fn().mockReturnValue(null),
unobserve: jest.fn().mockReturnValue(null),
disconnect: jest.fn().mockReturnValue(null)
})
window.IntersectionObserver = mockIntersectionObserver
})
我对 Jest+Typescript 进行了这样的测试
type CB = (arg1: IntersectionObserverEntry[]) => void;
class MockedObserver {
cb: CB;
options: IntersectionObserverInit;
elements: HTMLElement[];
constructor(cb: CB, options: IntersectionObserverInit) {
this.cb = cb;
this.options = options;
this.elements = [];
}
unobserve(elem: HTMLElement): void {
this.elements = this.elements.filter((en) => en !== elem);
}
observe(elem: HTMLElement): void {
this.elements = [...new Set(this.elements.concat(elem))];
}
disconnect(): void {
this.elements = [];
}
fire(arr: IntersectionObserverEntry[]): void {
this.cb(arr);
}
}
function traceMethodCalls(obj: object | Function, calls: any = {}) {
const handler: ProxyHandler<object | Function> = {
get(target, propKey, receiver) {
const targetValue = Reflect.get(target, propKey, receiver);
if (typeof targetValue === 'function') {
return function (...args: any[]) {
calls[propKey] = (calls[propKey] || []).concat(args);
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
return targetValue.apply(this, args);
};
} else {
return targetValue;
}
},
};
return new Proxy(obj, handler);
}
并在测试中
describe('useIntersectionObserver', () => {
let observer: any;
let mockedObserverCalls: { [k: string]: any } = {};
beforeEach(() => {
Object.defineProperty(window, 'IntersectionObserver', {
writable: true,
value: jest
.fn()
.mockImplementation(function TrackMock(
cb: CB,
options: IntersectionObserverInit
) {
observer = traceMethodCalls(
new MockedObserver(cb, options),
mockedObserverCalls
);
return observer;
}),
});
});
afterEach(() => {
observer = null;
mockedObserverCalls = {};
});
test('should do something', () => {
const mockedObserver = observer as unknown as MockedObserver;
const entry1 = {
target: new HTMLElement(),
intersectionRatio: 0.7,
};
// fire CB
mockedObserver.fire([entry1 as unknown as IntersectionObserverEntry]);
// possibly need to make test async/wait for see changes
// await waitForNextUpdate();
// await waitForDomChanges();
// await new Promise((resolve) => setTimeout(resolve, 0));
// Check calls to observer
expect(mockedObserverCalls.disconnect).toEqual([]);
expect(mockedObserverCalls.observe).toEqual([]);
});
});