首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用svelte /存储测试svelte组件

用svelte /存储测试svelte组件
EN

Stack Overflow用户
提问于 2020-04-01 12:40:28
回答 1查看 1.7K关注 0票数 8

当使用jest &@ test -library/ svelte测试svelte组件时,状态在测试之间是共享的,在每个测试之后是否会被删除,这样我就有了更多独立的单元测试。

商店/主题

代码语言:javascript
复制
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);

由于没有在测试之间共享存储的DI,所以我可以在beforeEach中将值重置为默认值,但试图查看是否有更好的解决方案。

ThemeSwitcher.spec.js

代码语言:javascript
复制
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();
});
EN

回答 1

Stack Overflow用户

发布于 2021-06-08 14:49:23

为了便于使用,我更喜欢将svelte商店包装在通用类中。

这是我的Store.ts

代码语言:javascript
复制
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())
    }
}

您可以扩展generic类来创建这样的新存储。

arrayStringStore.ts

代码语言:javascript
复制
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])
    }
}

例如:我有一个ArrayStringStore实例,即exampleStore

代码语言:javascript
复制
const exampleStore = new ArrayStringStore()

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

在你的测试文件里。

代码语言:javascript
复制
beforeEach(() => {
    exampleStore.reset()
})

注意:您可以通过exampleStore.get()获得存储的价值,而不需要在每个文件中都使用import { get } from svelte/store .

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60971089

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档