From 0f15cfc9e7170b2268ce71dccb162bdc20f97e87 Mon Sep 17 00:00:00 2001 From: afc163 Date: Wed, 12 Sep 2018 16:26:59 +0800 Subject: [PATCH] Fix authority reading and add test case --- src/utils/authority.js | 26 ++++++++++++-------------- src/utils/authority.test.js | 19 +++++++++++++++++++ src/utils/utils.js | 11 ----------- 3 files changed, 31 insertions(+), 25 deletions(-) create mode 100644 src/utils/authority.test.js diff --git a/src/utils/authority.js b/src/utils/authority.js index 8cd98658..3d2e7b34 100644 --- a/src/utils/authority.js +++ b/src/utils/authority.js @@ -1,24 +1,22 @@ -import { isJsonString } from '@/utils/utils'; - // use localStorage to store the authority info, which might be sent from server in actual project. -export function getAuthority() { +export function getAuthority(str) { // return localStorage.getItem('antd-pro-authority') || ['admin', 'user']; - const authorityString = localStorage.getItem('antd-pro-authority'); + const authorityString = + typeof str === 'undefined' ? localStorage.getItem('antd-pro-authority') : str; + // authorityString could be admin, "admin", ["admin"] let authority; - if (isJsonString(authorityString)) { + try { authority = JSON.parse(authorityString); - } else { - authority = [authorityString]; + } catch (e) { + authority = authorityString; + } + if (typeof authority === 'string') { + return [authority]; } return authority || ['admin']; } export function setAuthority(authority) { - let authorityString; - if (isJsonString(authority)) { - authorityString = JSON.stringify(authority); - } else { - authorityString = [authority]; - } - return localStorage.setItem('antd-pro-authority', authorityString); + const proAuthority = typeof authority === 'string' ? [authority] : authority; + return localStorage.setItem('antd-pro-authority', JSON.stringify(proAuthority)); } diff --git a/src/utils/authority.test.js b/src/utils/authority.test.js new file mode 100644 index 00000000..8a6cd41f --- /dev/null +++ b/src/utils/authority.test.js @@ -0,0 +1,19 @@ +import { getAuthority } from './authority'; + +describe('getAuthority should be strong', () => { + it('empty', () => { + expect(getAuthority(null)).toEqual(['admin']); // default value + }); + it('string', () => { + expect(getAuthority('admin')).toEqual(['admin']); + }); + it('array with double quotes', () => { + expect(getAuthority('"admin"')).toEqual(['admin']); + }); + it('array with single item', () => { + expect(getAuthority('["admin"]')).toEqual(['admin']); + }); + it('array with multiple items', () => { + expect(getAuthority('["admin", "guest"]')).toEqual(['admin', 'guest']); + }); +}); diff --git a/src/utils/utils.js b/src/utils/utils.js index bd47dbf7..3c795c22 100644 --- a/src/utils/utils.js +++ b/src/utils/utils.js @@ -181,14 +181,3 @@ export function formatWan(val) { export function isAntdPro() { return window.location.hostname === 'preview.pro.ant.design'; } - -export function isJsonString(str) { - try { - if (typeof JSON.parse(str) === 'object') { - return true; - } - } catch (e) { - return false; - } - return false; -} -- GitLab