Write test for nanostores

Write test for nanostores

why I wrote this post in the first place?

Lack of tut and doc around this awesome library that makes our lives easier.

Why and How we should do?

Why:

Because then we can separate logic and view from each other in a react application. Sounds cool, right. Just think about it, your HTML code is not full of logic & also your logic is not all over the place.

How:

Just create a file besides your normal tsx file and name whatever you like. I usually name it after my states.

Inside it you need to create your atom (or we can look at it the same way we were looking at the useState states)

import { atom, action } from 'nanostores

type User = { name: string }
const users = atom<User[]>([])

const addUser = action(users, "addUser", (users, user: User) => {
? users.set([...users.get(), user]);
})

export const UserStore = {
  states: { users },
  actions: { addUser }
}        

And now the test cases in Jest

import {
    allTasks,
    cleanStores,
    keepMount,
} from 'nanostores';

describe("UserStore", () => {
  afterEach(() => {
    // cleanStores means clean stores from listeners not reset the stores to their initial value
    cleanStores(UserStore.states.users)
    // To have a empty array inside the users state after each test
    UserStore.states.users.set([])
  })

  describe("addUser", () => {
    it("should add user", async () => {
      const user = {name: "Kasir"};
      keepMount(UserStore.states.users);
      UserStore.actions.addUser(user);
      // Just if the action is async - I mean the callback we passed to the action - which in our case is not. But I wrote it JFYI
      await allTasks();
      expect(UserStore.states.users.get()).toEqual(user)
    })
  })

})        

I hope this will help you to write a good app in React. :heart:


reference: https://github.com/nanostores/nanostores/issues/167

Michael Ferrara

?????Trusted IT Solutions Consultant | Technology | Science | Life | Author, Tech Topics | Goal: Give, Teach & Share | Featured Analyst on InformationWorth | TechBullion | CIO Grid | Small Biz Digest | GoDaddy

1 年

Mohammad, thanks for sharing!

要查看或添加评论,请登录

Mohammad Jawad barati的更多文章

  • My very own Linux cheat sheet

    My very own Linux cheat sheet

    HAL: Hardware Abstraction Layer do abstract hardwares for us /sys is where we can see what is connected to the system…

    1 条评论
  • ?????? ???? ?? ?????

    ?????? ???? ?? ?????

    ?? ???? ????: ???? ???? 43 ???? ?? (????? ?? ??????) ?? ??? ???? ???? ? ??????? ?????. ??? ??? ??????? ??? 56 ???? ??…

    2 条评论
  • Angular Material UI

    Angular Material UI

    IMO you need to know 3 things before reading this article: Authentication and Authorization in Angular. Whether you…

  • Create Angular project

    Create Angular project

    First please note that the following instructions are totally biased and is my favorite way of doing it. BTW if you…

  • Learn Angular fundamentals.

    Learn Angular fundamentals.

    Note that I am still a backend developer who is dealing with frontend stuff BTW it is not so hard. IMO Angular is much…

  • NestJS task scheduler

    NestJS task scheduler

    To tackle queue issues in NestJS we use @nestjs/schedule. With this package we can define declarative cron jobs.

  • OTP code in Node.js

    OTP code in Node.js

    Send a time-based OTP code - define TTL for the code Keep the symetric keys very safe Use approved cryptographic…

  • Refresh Token + Logout functionality in Node.js

    Refresh Token + Logout functionality in Node.js

    Here is how I implement refresh token in Node.js.

  • My journey in gPRC

    My journey in gPRC

    I am just fooling around gPRC. Please do not count this article as complete right now.

  • Node.js Design Patterns

    Node.js Design Patterns

    Here I just wanna share my notions and parts I think is important about this book. Please do not consider these article…

    1 条评论

社区洞察

其他会员也浏览了