2022-12-15 14:14:07 +08:00
|
|
|
import React from 'react';
|
2024-04-08 14:04:08 +08:00
|
|
|
|
2022-12-15 14:14:07 +08:00
|
|
|
import { render } from '../../../tests/utils';
|
2022-12-22 10:11:08 +08:00
|
|
|
import useResponsiveObserver from '../responsiveObserver';
|
2022-12-15 14:14:07 +08:00
|
|
|
|
|
|
|
describe('Test ResponsiveObserve', () => {
|
|
|
|
it('test ResponsiveObserve subscribe and unsubscribe', () => {
|
|
|
|
let responsiveObserveRef: any;
|
|
|
|
const Demo = () => {
|
2022-12-22 10:11:08 +08:00
|
|
|
const responsiveObserver = useResponsiveObserver();
|
|
|
|
responsiveObserveRef = responsiveObserver;
|
2022-12-15 14:14:07 +08:00
|
|
|
return null;
|
|
|
|
};
|
|
|
|
render(<Demo />);
|
2023-06-07 21:59:21 +08:00
|
|
|
const subscribeFunc = jest.fn();
|
2022-12-15 14:14:07 +08:00
|
|
|
const token = responsiveObserveRef.subscribe(subscribeFunc);
|
|
|
|
expect(
|
|
|
|
responsiveObserveRef.matchHandlers[responsiveObserveRef.responsiveMap.xs].mql.matches,
|
|
|
|
).toBeTruthy();
|
|
|
|
expect(subscribeFunc).toHaveBeenCalledTimes(1);
|
|
|
|
|
|
|
|
responsiveObserveRef.unsubscribe(token);
|
|
|
|
expect(
|
|
|
|
responsiveObserveRef.matchHandlers[responsiveObserveRef.responsiveMap.xs].mql.removeListener,
|
|
|
|
).toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
});
|