Testing svelte components with svelte/store

Joe*_*ner 6 javascript svelte svelte-component svelte-store testing-library

When testing svelte components, using jest & @testing-library/svelte, the state is shared between tests, is there away to tear down after each test so i have more isolated unit tests.

store/theme

import { writable } from "svelte/store";

export const LOCAL_STORAGE_KEY = "current:theme";

export const THEMES = {
  DARK: "dark",
  LIGHT: "light"
};

export const MATCH_DARK_THEME = "(prefers-color-scheme: dark)";

export const IS_USER_PREFERNCE_DARK =
  window.matchMedia && window.matchMedia(MATCH_DARK_THEME).matches;

export const DEFAULT_THEME =
  localStorage.getItem(LOCAL_STORAGE_KEY) || IS_USER_PREFERNCE_DARK
    ? THEMES.DARK
    : THEMES.LIGHT;

export const theme = writable(DEFAULT_THEME);

Run Code Online (Sandbox Code Playgroud)

because there is no DI the store is shared between tests, I could reset the value to default in the beforeEach but trying to see if there is a better solution.

ThemeSwitcher.spec.js

it("should be change body class on click", async () => {
  const { container } = render(ThemeSwitcher);

  expect(container.className).toEqual("theme-light");

  await fireEvent.click(getButton(container));

  expect(container.className).toEqual("theme-dark");
});

it("should render the sun if in light mode", async () => {
  const { getByText } = render(ThemeSwitcher);
  //default should be light mode but returns dark.
  const sun = getByText("Light theme on: Sun");

  expect(sun).toBeTruthy();
});
Run Code Online (Sandbox Code Playgroud)

小智 4

我更喜欢将 svelte store 包装在通用类中以便于使用。

这是我的Store.ts

import { writable, get, Writable } from "svelte/store"

/** Callback to inform of a value updates. */
export declare type Subscriber<T> = (value: T) => void
/** Unsubscribes from value updates. */
export declare type Unsubscriber = () => void
/** Callback to update a value. */
export declare type Updater<T> = (value: T) => T
/** Cleanup logic callback. */
export declare type Invalidator<T> = (value?: T) => void

class Store<T> implements Writable<T> {
    private intialValue: T
    private wrappee: Writable<T>

    // implements Writable
    subscribe: (run: Subscriber<T>, invalidate?: Invalidator<T>) => Unsubscriber
    set: (value: T) => void

    update: (updater: Updater<T>) => void

    constructor(value: T) {
        this.intialValue = value
        const _store = writable(value)
        const { subscribe, set, update } = _store
        this.subscribe = subscribe
        this.set = set
        this.update = update
        this.wrappee = _store
    }

    get() {
        return get(this.wrappee)
    }

    reset() {
        this.set(this.intialValue)
    }

    refresh() {
        this.set(this.get())
    }
}
Run Code Online (Sandbox Code Playgroud)

您可以扩展通用 Store 类来创建新商店。

arrayStringStore.ts

export default class ArrayStringStore extends Store<string[]> {
    constructor(arr: string[] = []) {
        super(arr)
    }

    // easy to add more convenience method
    add(item: string) {
        this.update(arr => [...arr, item])
    }
}
Run Code Online (Sandbox Code Playgroud)

例如:我有一个 ArrayStringStore 的实例,它是exampleStore

const exampleStore = new ArrayStringStore()
Run Code Online (Sandbox Code Playgroud)

您可以在每个测试用例之前轻松重置该存储的值

在你的测试文件中。

beforeEach(() => {
    exampleStore.reset()
})
Run Code Online (Sandbox Code Playgroud)

注意:您可以使用 获取存储值exampleStore.get(),不需要import { get } from svelte/store在每个文件中都如此。