@roots/container
Version:
Collections utility
408 lines (319 loc) • 10.6 kB
text/typescript
import {describe, expect, it, vi} from 'vitest'
import Container from './index.js'
describe(`container`, function () {
describe(`constructor`, () => {
it(`is constructable`, () => {
const container = new Container()
expect(container).toBeInstanceOf(Container)
})
})
describe(`get`, function () {
it(`returns undefined when no value is set`, () => {
const container = new Container()
expect(container.get(`foo`)).toEqual(undefined)
})
it(`returns the keyed value`, () => {
const repo = {foo: `bar`}
const container = new Container(repo)
expect(container.get(`foo`)).toBe(`bar`)
})
})
describe(`all`, function () {
it(`returns the raw repository`, () => {
const repo = {ergo: `dox`, foo: `bar`}
const container = new Container(repo)
expect(container.all()).toBe(repo)
})
})
describe(`set`, function () {
it(`returns itself`, () => {
const container = new Container()
expect(container.set(`foo`, `bar`)).toBeInstanceOf(Container)
})
it(`sets a value`, () => {
const container = new Container().set(`foo`, `bar`)
expect(container.get(`foo`)).toEqual(`bar`)
})
})
describe(`has`, () => {
it(`returns true if item found`, () => {
const container = new Container().set(`foo`, `bar`)
expect(container.has(`foo`)).toEqual(true)
})
it(`returns false if item not found`, () => {
const container = new Container().set(`foo`, `bar`)
expect(container.has(`neverSet`)).toEqual(false)
})
})
describe(`is`, function () {
it(`is: returns true if value equals predicate`, () => {
const repo = {test: 100}
const container = new Container(repo)
expect(container.is(`test`, 100)).toEqual(true)
})
it(`is: returns false if value does not equal predicate`, () => {
const repo = {test: 100}
const container = new Container(repo)
expect(container.is(`test`, `notIt`)).toEqual(false)
})
})
describe(`isTrue`, function () {
it(`returns true if keyed value is true`, () => {
const repo = {test: true}
const container = new Container(repo)
expect(container.isTrue(`test`)).toEqual(true)
})
it(`returns false if keyed value is false`, () => {
const repo = {test: false}
const container = new Container(repo)
expect(container.isTrue(`test`)).toEqual(false)
})
it(`returns false if keyed value is not boolean`, () => {
const repo = {test: `bar`}
const container = new Container(repo)
expect(container.isTrue(`test`)).toEqual(false)
})
})
describe(`isFalse`, function () {
it(`returns true if keyed value is false`, () => {
const repo = {test: false}
const container = new Container(repo)
expect(container.isFalse(`test`)).toEqual(true)
})
it(`returns false if keyed value is true`, () => {
const repo = {test: true}
const container = new Container(repo)
expect(container.isFalse(`test`)).toEqual(false)
})
it(`returns false if keyed value is not boolean`, () => {
const repo = {test: `bar`}
const container = new Container(repo)
expect(container.isFalse(`test`)).toEqual(false)
})
})
describe(`set`, function () {
it(`adds a value to the repository`, () => {
const repo = {foo: `bar`}
const container = new Container()
expect(container.set(`foo`, `bar`).all()).toEqual(repo)
})
})
describe(`isArray`, function () {
it(`returns true if value is an array`, () => {
const repo = {foo: [`bar`, `whiz`]}
const container = new Container(repo)
expect(container.isArray(`foo`)).toEqual(true)
})
})
describe(`isDefined`, function () {
it(`returns true if value is set`, () => {
const repo = {foo: [`bar`, `whiz`]}
const container = new Container(repo)
expect(container.isDefined(`foo`)).toEqual(true)
})
it(`returns false if value is not set`, () => {
const repo = {foo: [`bar`, `whiz`]}
const container = new Container(repo)
expect(container.isDefined(`bar`)).toEqual(false)
})
})
describe(`isNotArray`, function () {
it(`returns true if value is not an array`, () => {
const repo = {foo: `bar`}
const container = new Container(repo)
expect(container.isNotArray(`foo`)).toEqual(true)
})
})
describe(`isString`, function () {
it(`returns true if value is a String`, () => {
const repo = {foo: `bar`}
const container = new Container(repo)
expect(container.isString(`foo`)).toEqual(true)
})
})
describe(`isNotString`, function () {
it(`returns true if value is a String`, () => {
const repo = {foo: [`bar`, `whiz`]}
const container = new Container(repo)
expect(container.isNotString(`foo`)).toEqual(true)
})
})
describe(`isNumber`, function () {
it(`returns true if value is not a String`, () => {
const repo = {foo: 100}
const container = new Container(repo)
expect(container.isNumber(`foo`)).toEqual(true)
})
})
describe(`isNotNumber`, function () {
it(`returns true if value is not a Number`, () => {
const repo = {foo: [`bar`, `whiz`]}
const container = new Container(repo)
expect(container.isNotNumber(`foo`)).toEqual(true)
})
})
describe(`isNull`, function () {
it(`returns true if value is null`, () => {
const repo = {foo: null}
const container = new Container(repo)
expect(container.isNull(`foo`)).toEqual(true)
})
})
describe(`isNotNull`, function () {
it(`returns true if value is not null`, () => {
const repo = {foo: [`bar`, `whiz`]}
const container = new Container(repo)
expect(container.isNotNull(`foo`)).toEqual(true)
})
})
describe(`getMap`, function () {
it(`returns an instance of Map`, () => {
const repo = {anotherKey: `value2`, key: `value`}
const container = new Container(repo)
expect(container.getMap()).toBeInstanceOf(Map)
})
it(`returns Map with expected structure`, () => {
const repo = {anotherKey: `value2`, key: `value`}
const container = new Container(repo)
expect(Array.from(container.getMap())).toEqual(
expect.arrayContaining([
expect.arrayContaining([`key`, `value`]),
expect.arrayContaining([`anotherKey`, `value2`]),
]),
)
})
})
describe(`remove`, () => {
it(`removes a value`, () => {
const repo = {ergo: `dox`, foo: `bar`}
const container = new Container(repo)
container.remove(`foo`)
expect(container.all()).toEqual(
expect.objectContaining({
ergo: `dox`,
}),
)
})
})
describe(`setStore`, () => {
it(`sets the value of the repository property`, () => {
const repo = {ergo: `dox`, foo: `bar`}
const container = new Container(repo)
const replacement = {ben: `word`, oof: `yea`}
container.setStore(replacement)
expect(container.all()).toEqual(expect.objectContaining(replacement))
})
})
describe(`mutateStore`, () => {
it(`callback receives the store value`, () => {
const repo = {ergo: `dox`, foo: `bar`}
const container = new Container(repo)
container.mutateStore(store => {
expect(store).toEqual(expect.objectContaining(repo))
return store
})
})
it(`mutates the store using the provided callback`, () => {
const repo = {ergo: `dox`, foo: `bar`}
const container = new Container(repo)
const mutagen = store => Object.assign(store, {foo: `nea`})
container.mutateStore(mutagen)
expect(container.all()).toEqual({
ergo: `dox`,
foo: `nea`,
})
})
})
describe(`each`, () => {
it(`executes callback for each match`, () => {
const repo = {
foo: {
ergo: `dox`,
test: `bar`,
},
}
const container: Container = new Container(repo)
const callback = vi.fn()
container.each(`foo`, callback)
expect(callback).toBeCalledTimes(2)
})
})
describe(`every`, () => {
it(`executes callback for every top level key`, () => {
const repo = {
foo: {
ergo: `dox`,
test: `bar`,
},
}
const container: Container = new Container(repo)
const callback = vi.fn()
container.every(callback)
expect(callback).toBeCalledTimes(1)
})
})
describe(`transform`, () => {
it(`transforms a value using a callback`, () => {
const repo = {foo: 10}
const container = new Container(repo)
const result = container.transform(`foo`, v => {
return v + 10
})
expect(result).toEqual(20)
})
})
describe(`mutate`, () => {
it(`mutate a value using a callback`, () => {
const repo = {foo: 10}
const container = new Container(repo)
container.mutate(`foo`, v => {
return v + 10
})
expect(container.get(`foo`)).toEqual(20)
})
})
describe(`mergeStore`, () => {
it(`merges a value with the repository property`, () => {
const repo = {ergo: `dox`, foo: `bar`}
const container = new Container(repo)
expect(container.mergeStore({crash: `bandicoot`}).all()).toEqual({
...repo,
crash: `bandicoot`,
})
})
})
describe(`getEntries`, () => {
it(`retrieves repo as entries`, () => {
const repo = {ergo: `dox`, foo: `bar`}
const container = new Container(repo)
expect(container.getEntries()).toEqual(
expect.arrayContaining([
expect.arrayContaining([`foo`, `bar`]),
expect.arrayContaining([`ergo`, `dox`]),
]),
)
})
})
describe(`fromEntries`, () => {
it(`sets repo from entries`, () => {
const repo = {ergo: `dox`, foo: `bar`}
const container = new Container().fromEntries(Object.entries(repo))
expect(container.all()).toEqual(repo)
})
})
describe(`getKeys`, () => {
it(`returns array of keys`, () => {
const repo = {anotherKey: `value`, key: `value`}
const container = new Container(repo)
expect(container.getKeys()).toEqual(Object.keys(repo))
})
})
describe(`getValues`, () => {
it(`returns array of values`, () => {
const repo = {anotherKey: `value2`, key: `value`}
const container = new Container(repo)
expect(container.getValues()).toEqual(Object.values(repo))
})
})
})
export {}