差異處

這裏顯示兩個版本的差異處。

連向這個比對檢視

web:reactjs:test:teststaticmethod [2019/03/10 14:58]
tony [Problem]
web:reactjs:test:teststaticmethod [2023/06/25 09:48]
行 1: 行 1:
-====== React - How to test component with static method? ====== 
-===== Problem ===== 
-在撰寫程式時,我會習慣將同性質的utility method放在同一個class中。以實作Login、Logout的功能來說,我就會建立一個class叫Auth,會包含以下methods:​ 
-<code javascript>​ 
-import React, { Component } from '​react';​ 
- 
-const session_token_key = '​session_token';​ 
- 
-export default class Auth extends Component { 
-    ​ 
-    static signIn(username,​ password){ 
-        // check credential 
-        sessionStorage.setItem(session_token_key,​ '​session_info'​);​ 
-    } 
- 
-    static signOut(){ 
-        sessionStorage.clear();​ 
-    } 
- 
-    static checkLogin(){ 
-        if( !sessionStorage.getItem(session_token_key) ) { 
-            throw new Error("​no session info"​);​ 
-        } 
-    } 
-}; 
-</​code>​ 
- 
-===== How to? ===== 
-