From 375ded92ef43e8c4735a4d9e2e094c67258f3f60 Mon Sep 17 00:00:00 2001 From: Bruno Motta Date: Thu, 18 Mar 2021 22:30:50 -0300 Subject: [PATCH] test: usePrevious hook (#13554) --- .../src/common/hooks/usePrevious/index.ts | 20 +++++++++ .../hooks/usePrevious/usePrevious.test.ts | 43 +++++++++++++++++++ .../hooks/{ => usePrevious}/usePrevious.ts | 0 3 files changed, 63 insertions(+) create mode 100644 superset-frontend/src/common/hooks/usePrevious/index.ts create mode 100644 superset-frontend/src/common/hooks/usePrevious/usePrevious.test.ts rename superset-frontend/src/common/hooks/{ => usePrevious}/usePrevious.ts (100%) diff --git a/superset-frontend/src/common/hooks/usePrevious/index.ts b/superset-frontend/src/common/hooks/usePrevious/index.ts new file mode 100644 index 000000000..a56027844 --- /dev/null +++ b/superset-frontend/src/common/hooks/usePrevious/index.ts @@ -0,0 +1,20 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +export * from './usePrevious'; diff --git a/superset-frontend/src/common/hooks/usePrevious/usePrevious.test.ts b/superset-frontend/src/common/hooks/usePrevious/usePrevious.test.ts new file mode 100644 index 000000000..c433d7a02 --- /dev/null +++ b/superset-frontend/src/common/hooks/usePrevious/usePrevious.test.ts @@ -0,0 +1,43 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +import { renderHook } from '@testing-library/react-hooks'; +import { usePrevious } from './usePrevious'; + +test('get undefined on the first render when initialValue is not defined', () => { + const hook = renderHook(() => usePrevious('state')); + expect(hook.result.current).toBeUndefined(); +}); + +test('get initial value on the first render when initialValue is defined', () => { + const hook = renderHook(() => usePrevious('state', 'initial')); + expect(hook.result.current).toBe('initial'); +}); + +test('get state value on second render', () => { + const hook = renderHook(() => usePrevious('state', 'initial')); + hook.rerender(() => usePrevious('state')); + expect(hook.result.current).toBe('state'); +}); + +test('get state value on third render', () => { + const hook = renderHook(() => usePrevious('state')); + hook.rerender(() => usePrevious('state')); + hook.rerender(() => usePrevious('state-2')); + expect(hook.result.current).toBe('state'); +}); diff --git a/superset-frontend/src/common/hooks/usePrevious.ts b/superset-frontend/src/common/hooks/usePrevious/usePrevious.ts similarity index 100% rename from superset-frontend/src/common/hooks/usePrevious.ts rename to superset-frontend/src/common/hooks/usePrevious/usePrevious.ts