Set up vitest and add tests for interactive

This commit is contained in:
Nate Butler
2023-06-20 16:05:23 -07:00
committed by Mikayla Maki
parent 4bd89c4c8c
commit 5369f2c25a
25 changed files with 4545 additions and 2162 deletions
+3
View File
@@ -0,0 +1,3 @@
import { interactive } from "./interactive";
export { interactive }
@@ -0,0 +1,59 @@
import { NOT_ENOUGH_STATES_ERROR, NO_DEFAULT_OR_BASE_ERROR, interactive } from './interactive'
import { describe, it, expect } from 'vitest'
describe('interactive', () => {
it('creates an Interactive<Element> with base properties and states', () => {
const result = interactive({
base: { fontSize: 10, color: '#FFFFFF' },
state: {
hovered: { color: '#EEEEEE' },
clicked: { color: '#CCCCCC' },
}
})
expect(result).toEqual({
default: { color: '#FFFFFF', fontSize: 10 },
hovered: { color: '#EEEEEE', fontSize: 10 },
clicked: { color: '#CCCCCC', fontSize: 10 },
})
})
it('creates an Interactive<Element> with no base properties', () => {
const result = interactive({
state: {
default: { color: '#FFFFFF', fontSize: 10 },
hovered: { color: '#EEEEEE' },
clicked: { color: '#CCCCCC' },
}
})
expect(result).toEqual({
default: { color: '#FFFFFF', fontSize: 10 },
hovered: { color: '#EEEEEE', fontSize: 10 },
clicked: { color: '#CCCCCC', fontSize: 10 },
})
})
it('throws error when both default and base are missing', () => {
const state = {
hovered: { color: 'blue' },
}
expect(() => interactive({ state })).toThrow(
NO_DEFAULT_OR_BASE_ERROR
)
})
it('throws error when no other state besides default is present', () => {
const state = {
default: { fontSize: 10 },
}
expect(() => interactive({ state })).toThrow(
NOT_ENOUGH_STATES_ERROR
)
})
})
@@ -0,0 +1,74 @@
import merge from "ts-deepmerge"
type InteractiveState = "default" | "hovered" | "clicked" | "selected" | "disabled";
type Interactive<T> = {
default: T,
hovered?: T,
clicked?: T,
selected?: T,
disabled?: T,
};
export const NO_DEFAULT_OR_BASE_ERROR = "An interactive object must have a default state, or a base property."
export const NOT_ENOUGH_STATES_ERROR = "An interactive object must have a default and at least one other state."
interface InteractiveProps<T> {
base?: T,
state: Partial<Record<InteractiveState, T>>
}
/**
* Helper function for creating Interactive<T> objects that works with Toggle<T>-like behavior.
* It takes a default object to be used as the value for `default` field and fills out other fields
* with fields from either `base` or from the `state` object which contains values for specific states.
* Notably, it does not touch `hover`, `clicked`, `selected` and `disabled` states if there are no modifications for them.
*
* @param defaultObj Object to be used as the value for the `default` field.
* @param base Optional object containing base fields to be included in the resulting object.
* @param state Object containing optional modified fields to be included in the resulting object for each state.
* @returns Interactive<T> object with fields from `base` and `state`.
*/
export function interactive<T extends Object>({ base, state }: InteractiveProps<T>): Interactive<T> {
if (!base && !state.default) throw new Error(NO_DEFAULT_OR_BASE_ERROR);
let defaultState: T;
if (state.default && base) {
defaultState = merge(base, state.default) as T;
} else {
defaultState = base ? base : state.default as T;
}
let interactiveObj: Interactive<T> = {
default: defaultState,
};
let stateCount = 0;
if (state.hovered !== undefined) {
interactiveObj.hovered = merge(interactiveObj.default, state.hovered) as T;
stateCount++;
}
if (state.clicked !== undefined) {
interactiveObj.clicked = merge(interactiveObj.default, state.clicked) as T;
stateCount++;
}
if (state.selected !== undefined) {
interactiveObj.selected = merge(interactiveObj.default, state.selected) as T;
stateCount++;
}
if (state.disabled !== undefined) {
interactiveObj.disabled = merge(interactiveObj.default, state.disabled) as T;
stateCount++;
}
if (stateCount < 1) {
throw new Error(NOT_ENOUGH_STATES_ERROR);
}
return interactiveObj;
}