@gameroom/kit
Version:
Node kit for the Gameroom API
104 lines (99 loc) • 3.02 kB
JavaScript
const { faker } = require('@faker-js/faker')
const chai = require('chai')
const {
models: { Product, Price, Unit }
} = require('../../')
const should = chai.should()
let products
describe('Base', () => {
describe('setup', () => {
it('should get a collection', async () => {
let error
// user
try {
products = await Product.get({ limit: 2 })
} catch (err) {
error = err
}
should.not.exist(error)
should.exist(products)
products.should.be.a('array')
})
})
describe('Base.batch', () => {
it('should get array of arrays', async () => {
let batches, error
// user
try {
batches = products.batch(1)
} catch (err) {
error = err
}
should.not.exist(error)
should.exist(batches)
batches.should.be.a('array')
should.equal(batches.length, 2)
should.equal(batches[0].length, 1)
})
})
describe('Base.get', () => {
it('should get children', async () => {
let prices, error, units
// user
try {
prices = await products.get(Price)
units = await prices.get(Unit, { filter: { key: 'amount', comparison: '>=', value: 0 } })
} catch (err) {
error = err
}
should.not.exist(error)
should.exist(products)
products.should.be.a('array')
should.exist(prices)
prices.should.be.a('array')
should.exist(units)
units.should.be.a('array')
const fault = prices.reduce((a, i) => (products.find((p) => p.id === i.product_id) ? a : true), false) || units.reduce((a, i) => (products.find((p) => p.id === i.product_id) ? a : true), false) || units.reduce((a, i) => (prices.find((p) => p.id === i.price_id) ? a : true), false)
should.equal(fault, false)
})
})
describe('Base.refresh', () => {
it('should refresh all', async () => {
let error
const value = faker.random.word()
const id = products[0].id
const properties = products[0].properties
properties.test_key = value
// user
try {
await Product.update({ id, properties })
products = await products.refresh()
} catch (err) {
error = err
}
should.not.exist(error)
should.equal(products.length, 2)
const fault = products.find((i) => i.id === id)?.properties.test_key !== value
should.equal(fault, false)
})
})
describe('Base.save', () => {
it('should save all', async () => {
let error
const value = faker.random.word()
// user
try {
for (const p of products) p.properties.test_key = value
products = await products.save()
products = await products.refresh()
} catch (err) {
error = err
}
should.not.exist(error)
should.exist(products)
should.equal(products.length, 2)
const fault = products.reduce((a, i) => (i.properties.test_key === value ? a : true), false)
should.equal(fault, false)
})
})
})