[fix] AppState.removeEventListener deprecation

Fix #2215
Fix #2166
This commit is contained in:
Nicolas Gallagher
2022-02-18 10:10:55 -08:00
parent 79707ce1b3
commit 17241dbcfa
2 changed files with 8 additions and 16 deletions
@@ -10,23 +10,10 @@ describe('apis/AppState', () => {
expect(() => AppState.addEventListener('foo', handler)).toThrow();
expect(() => AppState.addEventListener('change', handler).remove()).not.toThrow();
});
});
describe('removeEventListener', () => {
beforeEach(() => {
// removeEventListener logs a deprecation warning, ignore
jest.spyOn(console, 'error');
console.error.mockImplementation(() => {});
});
afterEach(() => {
console.error.mockRestore();
});
test('throws if the provided "eventType" is not supported', () => {
AppState.addEventListener('change', handler);
expect(() => AppState.removeEventListener('foo', handler)).toThrow();
expect(() => AppState.removeEventListener('change', handler)).not.toThrow();
test('returns remove subscription', () => {
const subscription = AppState.addEventListener('change', handler);
expect(() => subscription.remove()).not.toThrow();
});
});
});
@@ -72,6 +72,11 @@ export default class AppState {
static removeEventListener(type: string, handler: Function) {
if (AppState.isAvailable) {
console.error(
`AppState.removeListener('${type}', ...): Method has been ` +
'deprecated. Please instead use `remove()` on the subscription ' +
'returned by `AppState.addEventListener`.'
);
invariant(
EVENT_TYPES.indexOf(type) !== -1,
'Trying to remove listener for unknown event: "%s"',