2022-12-15 14:14:07 +08:00
|
|
|
import React from 'react';
|
|
|
|
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 />);
|
|
|
|
const subscribeFunc = jest.fn();
|
|
|
|
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();
|
|
|
|
});
|
|
|
|
});
|