-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'dev' of https://github.com/sanghunlee-711/iron-mate int…
…o main
- Loading branch information
Showing
9 changed files
with
208 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
import { STORAGE } from '@/app/constants/message'; | ||
import CustomAlert from '@/app/utils/alert'; | ||
import DataStorage from '../storage'; | ||
|
||
class MockJSON { | ||
originStringify = JSON.stringify; | ||
originParse = JSON.parse; | ||
|
||
mockStringify(mockValue: any) { | ||
JSON.stringify = mockValue; | ||
} | ||
|
||
mockParse(mockValue: any) { | ||
JSON.parse = mockValue; | ||
} | ||
|
||
clearMock() { | ||
JSON.stringify = this.originStringify; | ||
JSON.parse = this.originParse; | ||
} | ||
} | ||
|
||
const MOCK = { | ||
DATA: { | ||
KEY: 'test-key', | ||
VALUE: 'test-value', | ||
}, | ||
}; | ||
|
||
describe('DataStorage클래스 테스트', () => { | ||
const setup = () => { | ||
const customAlert = new CustomAlert(); | ||
const dataStorage = new DataStorage(customAlert); | ||
const mockJSON = new MockJSON(); | ||
|
||
jest.spyOn(customAlert, 'toast'); | ||
|
||
return { | ||
dataStorage, | ||
customAlert, | ||
mockJSON, | ||
}; | ||
}; | ||
|
||
beforeEach(() => { | ||
window.localStorage.clear(); | ||
}); | ||
|
||
describe('set > 데이터 저장', () => { | ||
it('올바르지 않은 데이터 형태의 경우 alert를 발생시켜야 한다.', () => { | ||
const { dataStorage, customAlert } = setup(); | ||
dataStorage.set(MOCK.DATA.KEY, null); | ||
|
||
expect(customAlert.toast).toHaveBeenCalledWith(STORAGE.INCORRECT_DATA); | ||
}); | ||
|
||
it('알 수 없는 이유로 데이터 저장에 실패한 경우 alert를 발생시켜야 한다.', () => { | ||
const { dataStorage, customAlert, mockJSON } = setup(); | ||
|
||
mockJSON.mockStringify( | ||
jest.fn(() => { | ||
throw new Error(); | ||
}) | ||
); | ||
|
||
dataStorage.set(MOCK.DATA.KEY, MOCK.DATA.VALUE); | ||
expect(customAlert.toast).toHaveBeenCalledWith(STORAGE.SAVE_FAILURE); | ||
mockJSON.clearMock(); | ||
}); | ||
|
||
it('데이터 저장에 성공한 경우 alert를 발생시켜야 한다.', () => { | ||
const { dataStorage, customAlert } = setup(); | ||
|
||
dataStorage.set(MOCK.DATA.KEY, MOCK.DATA.VALUE); | ||
|
||
expect(customAlert.toast).toHaveBeenCalledWith(STORAGE.SAVE_SUCCESS); | ||
}); | ||
}); | ||
|
||
describe('get > 데이터 가져오기', () => { | ||
it('key에 해당하는 데이터가 없는 경우 null을 반환해준다.', () => { | ||
const { dataStorage } = setup(); | ||
|
||
expect(dataStorage.get(MOCK.DATA.KEY)).toBeNull(); | ||
}); | ||
|
||
it('key에 해당하는 데이터가 있는 경우 값을 반환해준다.', () => { | ||
const { dataStorage } = setup(); | ||
|
||
dataStorage.set(MOCK.DATA.KEY, MOCK.DATA.VALUE); | ||
expect(dataStorage.get(MOCK.DATA.KEY)).toBe(MOCK.DATA.VALUE); | ||
}); | ||
|
||
it('알 수 없는 이유로 데이터 가져오기에 실패한 경우 alert를 발생시켜야 한다.', () => { | ||
const { dataStorage, customAlert, mockJSON } = setup(); | ||
|
||
mockJSON.mockParse( | ||
jest.fn(() => { | ||
throw new Error(); | ||
}) | ||
); | ||
|
||
dataStorage.set(MOCK.DATA.KEY, MOCK.DATA.VALUE); | ||
dataStorage.get(MOCK.DATA.KEY); | ||
|
||
expect(customAlert.toast).toHaveBeenCalledWith(STORAGE.GET_FAILURE); | ||
mockJSON.clearMock(); | ||
}); | ||
}); | ||
|
||
describe('remove > 데이터 삭제', () => { | ||
it('데이터 삭제 완료 후 alert를 발생시킨다.', () => { | ||
const { dataStorage, customAlert } = setup(); | ||
|
||
dataStorage.set(MOCK.DATA.KEY, MOCK.DATA.VALUE); | ||
dataStorage.remove(MOCK.DATA.KEY); | ||
|
||
expect(customAlert.toast).toHaveBeenCalledWith(STORAGE.REMOVE_SUCCESS); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import { STORAGE } from '../constants/message'; | ||
import { IAlert } from '../interface/alert'; | ||
import CustomAlert from '../utils/alert'; | ||
|
||
class DataStorage { | ||
customAlert: IAlert; | ||
|
||
constructor(alertInstance: IAlert = new CustomAlert()) { | ||
this.customAlert = alertInstance; | ||
} | ||
|
||
set(key: string = 'iron-mate-data', data: any) { | ||
try { | ||
if (!data || !key) throw new Error(STORAGE.INCORRECT_DATA); | ||
|
||
global?.window?.localStorage.setItem(key, JSON.stringify(data)); | ||
this.customAlert.toast(STORAGE.SAVE_SUCCESS); | ||
} catch (e) { | ||
const error = e as Error; | ||
this.customAlert.toast(error.message || STORAGE.SAVE_FAILURE); | ||
console.error(e); | ||
} | ||
} | ||
|
||
get(key: string = 'iron-mate-data') { | ||
try { | ||
const keyData = global?.window?.localStorage.getItem(key) || ''; | ||
|
||
if (!keyData) return null; | ||
|
||
const data = JSON.parse(keyData as string); | ||
return data; | ||
} catch (e) { | ||
this.customAlert.toast(STORAGE.GET_FAILURE); | ||
console.error(e); | ||
} | ||
} | ||
|
||
remove(key: string = 'iron-mate-data') { | ||
try { | ||
global?.window?.localStorage.removeItem(key); | ||
this.customAlert.toast(STORAGE.REMOVE_SUCCESS); | ||
} catch (e) { | ||
this.customAlert.toast(STORAGE.REMOVE_FAILURE); | ||
console.error(e); | ||
} | ||
} | ||
} | ||
|
||
export default DataStorage; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
export const MESSAGE = {}; | ||
|
||
export const STORAGE = { | ||
SAVE_SUCCESS: '브라우저 내 데이터 저장이 완료되었습니다.', | ||
SAVE_FAILURE: '브라우저 내 데이터 저장에 실패하였습니다.', | ||
INCORRECT_DATA: '올바르지 않은 데이터 형태입니다.', | ||
GET_FAILURE: '브라우저 내 데이터 가져오기에 실패하였습니다.', | ||
REMOVE_SUCCESS: '브라우저 내 데이터 삭제가 완료되었습니다.', | ||
REMOVE_FAILURE: '브라우저 내 데이터 삭제에 실패하였습니다.', | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export interface IAlert { | ||
toast: (message: string) => void; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters