Better mockItems.

This commit is contained in:
Sage Vaillancourt 2022-09-06 21:01:40 -04:00
parent 81e18d4e60
commit ddc4576f94
1 changed files with 35 additions and 29 deletions

View File

@ -1,6 +1,6 @@
// noinspection JSCheckFunctionSignatures // noinspection JSCheckFunctionSignatures
import { writable } from 'svelte/store' import { get, writable } from 'svelte/store'
import { backendAddressAndPort, queryMode, queryState } from './constants.js' import { backendAddressAndPort, queryMode, queryState } from './constants.js'
const getRandomFromArray = array => array[Math.floor(Math.random() * array.length)] const getRandomFromArray = array => array[Math.floor(Math.random() * array.length)]
@ -16,25 +16,34 @@ const addMetadata = mockItem => ({
key: null, key: null,
}) })
const mockItems = () => [ const mockUsernames = [
{ 'sagev', 'cameronl', 'westonm', 'connorl', 'cameronf', 'thomase'
eventType: "purchase", ]
userName: "sagev",
itemName: "Bean Enchilada", const mockEventTypes = [
quantity: getRandomFromArray([1, 2, 3]) 'purchase', 'refund',
}, ]
{
eventType: "purchase", const mockItemTypes = [
userName: "cameronl", 'Taco', 'Enchilada', 'Soda', 'Chalupa', 'Tostada'
itemName: "Tacos", ]
quantity: getRandomFromArray([1, 2, 3])
}, const buildMockItem = () => {
{ const purchasedBy = getRandomFromArray(mockUsernames)
eventType: "refund", let transactionHandledBy = getRandomFromArray(mockUsernames)
userName: "sagev", while (transactionHandledBy === purchasedBy) {
purchaseId: crypto.randomUUID(), transactionHandledBy = getRandomFromArray(mockUsernames)
}, }
].map(addMetadata) return addMetadata({
eventType: getRandomFromArray(mockEventTypes),
itemType: getRandomFromArray(mockItemTypes),
purchasedBy,
transactionHandledBy,
purchaseId: crypto.randomUUID()
});
}
const mockItems = Array(100).fill(buildMockItem, 0).map(mocker => mocker())
export const testMode = true export const testMode = true
@ -71,7 +80,7 @@ const testQuery = (mode) => {
queryStartTime: new Date() queryStartTime: new Date()
})) }))
const addItem = () => { const addItem = () => {
const item = getRandomFromArray(mockItems()) const item = buildMockItem()
if (filterFunc(item, item.value, JSON.stringify(item))) { if (filterFunc(item, item.value, JSON.stringify(item))) {
item.timestamp = new Date().getTime() item.timestamp = new Date().getTime()
state.update(s => ({ state.update(s => ({
@ -85,19 +94,16 @@ const testQuery = (mode) => {
} }
} }
setTimeout(addItem, testTimeout) setTimeout(addItem, testTimeout)
} else { } else { // ONE_SHOT
state.update(s => ({
...s,
}))
setTimeout(() => { setTimeout(() => {
const items = mockItems().filter(item => filterFunc(item, item.value, JSON.stringify(item))) const filteredItems = mockItems.filter(item => filterFunc(item, item.value, JSON.stringify(item))).slice(0, itemLimit)
state.update(s => ({ state.update(s => ({
...s, ...s,
items, items: filteredItems,
itemCount: items.length, itemCount: filteredItems.length,
queryState: queryState.DONE queryState: queryState.DONE
})) }))
}, testTimeout * 10) }, 2000)
} }
} catch (e) { } catch (e) {
console.log('Caught an error:', e.toString()) console.log('Caught an error:', e.toString())