Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow
*/

import * as React from 'react';
import { render } from '@testing-library/react';
import Appearance from '../../Appearance';
import useColorScheme from '..';

describe('useColorScheme', () => {
test('keeps its subscription active across rerenders', () => {
const remove = jest.fn();
const addChangeListener = jest
.spyOn(Appearance, 'addChangeListener')
.mockReturnValue({ remove });

function Component({ label }): React.Node {
const colorScheme = useColorScheme();
return <div>{`${label}:${colorScheme}`}</div>;
}

const { rerender, unmount } = render(<Component label="first" />);
expect(addChangeListener).toHaveBeenCalledTimes(1);
expect(remove).not.toHaveBeenCalled();

rerender(<Component label="second" />);
expect(addChangeListener).toHaveBeenCalledTimes(1);
expect(remove).not.toHaveBeenCalled();

unmount();
expect(remove).toHaveBeenCalledTimes(1);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export default function useColorScheme(): ColorSchemeName {
}
const { remove } = Appearance.addChangeListener(listener);
return remove;
});
}, []);

return colorScheme;
}
Loading