
import MyStore from '../src/renderer/store/MyStore'
/* The state methods within this get overridden by jest.spyOn(), as below. */
jest.mock('../src/renderer/store/MyStore', () => jest.fn())
export class MyStoreMock {
/**
* Overrides specific items in the MyStore. This gets called by each test
* to ensure they are each using different spies & values.
*
* @static
* @param {Map} mockStoreItems
* @param {*} uniqueID
* @memberof MyStoreMock
*/
public static init(mockStoreItems: Map, uniqueID: any) {
/* Useful for initialising the store's items with unique values. */
if (uniqueID) {
for (const key of mockStoreItems) {
mockStoreItems.set(key[0], uniqueID + key[0])
}
}
jest.spyOn(MyStore, 'commit').mockImplementation((commitItem: any,
newValue: any) => {
mockStoreItems.set(commitItem, newValue)
})
jest.spyOn(MyStore, 'getters', 'get').mockImplementation(() => {
/* We must generate this now to include 'commit' changes etc. */
let getters = {}
for (const key of mockStoreItems) {
getters[key[0]] = key[1]
}
return getters
})
}
}import { PinkFloyd } from '../../src/renderer/model/PinkFloyd'
import { MyStoreMock } from '../../MyStoreMock'
describe('Mock Example', () => {
test('MyStoreMock', () => {
let mockStoreItems = new Map()
/* This value will get set using the global unique ID on the next line.
* The production store has a value of 'The Endless River' */
mockStoreItems.set('worstAlbum', '')
MyStoreMock.init(mockStoreItems, "There isn't one")
/* The production store has a value of 'Wish You Were Here' */
mockStoreItems.set('bestAlbum', 'Dark Side of the Moon')
/* Modules can be mocked as well.
* The production store has a value of 'David, Rick, Roger, Nick' */
mockStoreItems.set('Members/names', ['John', 'Paul', 'Whoops'])
const pinkFloyd = new PinkFloyd()
/* This sets all the data that we're testing below */
pinkFloyd.getData()
expect(pinkFloyd.worstAlbum).toBe('There isn't one')
expect(pinkFloyd.bestAlbum).toBe('Dark Side of the Moon')
expect(pinkFloyd.members).toContain('Whoops')
})
})
Designed to improve your software development life, this is a full list of our coding help pages.
There's more to assist you, with Git tips and best software development practices, on our resources page.

