-
Notifications
You must be signed in to change notification settings - Fork 60
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #995 from Jeezman/mock-data-test
Add test case for main store
- Loading branch information
Showing
9 changed files
with
177 additions
and
9 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
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,27 @@ | ||
export const localStorageMock = (function () { | ||
let store = {}; | ||
|
||
return { | ||
getItem(key: string | number) { | ||
return store[key]; | ||
}, | ||
|
||
setItem(key: string | number, value: any) { | ||
store[key] = value; | ||
}, | ||
|
||
clear() { | ||
store = {}; | ||
}, | ||
|
||
removeItem(key: string | number) { | ||
delete store[key]; | ||
}, | ||
|
||
getAll() { | ||
return store; | ||
} | ||
}; | ||
})(); | ||
|
||
Object.defineProperty(window, 'localStorage', { value: localStorageMock }); |
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,43 @@ | ||
import { localStorageMock } from '../__test__/__mockData__/localStorage'; | ||
describe('Local Storage', () => { | ||
beforeEach(() => { | ||
Object.defineProperty(window, 'localStorage', { value: localStorageMock }); | ||
}); | ||
|
||
afterEach(() => { | ||
localStorageMock.clear(); | ||
}); | ||
|
||
it('should set and get an item from local storage', () => { | ||
const key = 'testKey'; | ||
const value = 'testValue'; | ||
|
||
localStorageMock.setItem(key, value); | ||
|
||
expect(localStorageMock.getItem(key)).toEqual(value); | ||
}); | ||
|
||
it('should remove an item from local storage', () => { | ||
const key = 'testKey'; | ||
const value = 'testValue'; | ||
|
||
localStorageMock.setItem(key, value); | ||
localStorageMock.removeItem(key); | ||
|
||
expect(localStorageMock.getItem(key)).toBeUndefined(); | ||
}); | ||
|
||
it('should clear all items from local storage', () => { | ||
const key1 = 'testKey1'; | ||
const value1 = 'testValue1'; | ||
const key2 = 'testKey2'; | ||
const value2 = 'testValue2'; | ||
|
||
localStorageMock.setItem(key1, value1); | ||
localStorageMock.setItem(key2, value2); | ||
localStorageMock.clear(); | ||
|
||
expect(localStorageMock.getItem(key1)).toBeUndefined(); | ||
expect(localStorageMock.getItem(key2)).toBeUndefined(); | ||
}); | ||
}); |
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 |
---|---|---|
@@ -1 +1 @@ | ||
export * from './helpers'; | ||
export * from './helpers.tsx'; |
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,74 @@ | ||
import { toJS } from 'mobx'; | ||
import { user } from '../../__test__/__mockData__/user'; | ||
import { uiStore } from '../ui'; | ||
import { MainStore } from '../main'; | ||
import { localStorageMock } from '../../__test__/__mockData__/localStorage'; | ||
|
||
const mockFetch = jest.fn(); | ||
const mockHeaders = jest.fn(); | ||
|
||
const origFetch = global.fetch; | ||
|
||
beforeAll(() => { | ||
global.fetch = mockFetch; | ||
global.Headers = mockHeaders; | ||
}); | ||
|
||
afterAll(() => { | ||
global.fetch = origFetch; | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
describe('Main store', () => { | ||
beforeEach(async () => { | ||
uiStore.setMeInfo(user); | ||
localStorageMock.setItem('ui', JSON.stringify(uiStore)); | ||
}); | ||
|
||
it('should call endpoint on saveBounty', () => { | ||
const mainStore = new MainStore(); | ||
mainStore.saveBounty = jest | ||
.fn() | ||
.mockReturnValueOnce(Promise.resolve({ status: 200, message: 'success' })); | ||
const bounty = { | ||
body: { | ||
title: 'title', | ||
description: 'description', | ||
amount: 100, | ||
owner_pubkey: user.owner_pubkey, | ||
owner_alias: user.alias, | ||
owner_contact_key: user.contact_key, | ||
owner_route_hint: user.route_hint ?? '', | ||
extras: user.extras, | ||
price_to_meet: user.price_to_meet, | ||
img: user.img, | ||
tags: [], | ||
route_hint: user.route_hint | ||
} | ||
}; | ||
mainStore.saveBounty(bounty); | ||
expect(mainStore.saveBounty).toBeCalledWith({ | ||
body: bounty.body | ||
}); | ||
}); | ||
|
||
it('should save user profile', async () => { | ||
const mainStore = new MainStore(); | ||
const person = { | ||
owner_pubkey: user.owner_pubkey, | ||
owner_alias: user.alias, | ||
owner_contact_key: user.contact_key, | ||
owner_route_hint: user.route_hint ?? '', | ||
description: user.description, | ||
extras: user.extras, | ||
price_to_meet: user.price_to_meet, | ||
img: user.img, | ||
tags: [], | ||
route_hint: user.route_hint | ||
}; | ||
mainStore.saveProfile(person); | ||
|
||
expect(toJS(uiStore.meInfo)).toEqual(user); | ||
expect(localStorageMock.getItem('ui')).toEqual(JSON.stringify(uiStore)); | ||
}); | ||
}); |
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,9 @@ | ||
module.exports = { | ||
process() { | ||
return { code: 'module.exports = {};' }; | ||
}, | ||
getCacheKey() { | ||
// The output is always the same. | ||
return 'svgTransform'; | ||
}, | ||
}; |
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