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