You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@superset.apache.org by yj...@apache.org on 2021/03/19 01:31:51 UTC

[superset] branch master updated: test: usePrevious hook (#13554)

This is an automated email from the ASF dual-hosted git repository.

yjc pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/superset.git


The following commit(s) were added to refs/heads/master by this push:
     new 375ded9  test: usePrevious hook (#13554)
375ded9 is described below

commit 375ded92ef43e8c4735a4d9e2e094c67258f3f60
Author: Bruno Motta <wa...@gmail.com>
AuthorDate: Thu Mar 18 22:30:50 2021 -0300

    test: usePrevious hook (#13554)
---
 .../hooks/{usePrevious.ts => usePrevious/index.ts} | 18 +--------
 .../common/hooks/usePrevious/usePrevious.test.ts   | 43 ++++++++++++++++++++++
 .../common/hooks/{ => usePrevious}/usePrevious.ts  |  0
 3 files changed, 44 insertions(+), 17 deletions(-)

diff --git a/superset-frontend/src/common/hooks/usePrevious.ts b/superset-frontend/src/common/hooks/usePrevious/index.ts
similarity index 56%
copy from superset-frontend/src/common/hooks/usePrevious.ts
copy to superset-frontend/src/common/hooks/usePrevious/index.ts
index 178cbc0..a560278 100644
--- a/superset-frontend/src/common/hooks/usePrevious.ts
+++ b/superset-frontend/src/common/hooks/usePrevious/index.ts
@@ -17,20 +17,4 @@
  * under the License.
  */
 
-import { useEffect, useRef } from 'react';
-
-/**
- * Pass in a piece of state.
- * This hook returns what the value of that state was in the previous render.
- * Returns undefined (or whatever value you specify) the first time.
- */
-export function usePrevious<T>(value: T): T | undefined;
-export function usePrevious<T, INIT>(value: T, initialValue: INIT): T | INIT;
-export function usePrevious<T>(value: T, initialValue?: any): T {
-  const previous = useRef<T>(initialValue);
-  useEffect(() => {
-    // useEffect runs after the render completes
-    previous.current = value;
-  }, [value]);
-  return previous.current;
-}
+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 0000000..c433d7a
--- /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