Effect CommunityEC
Effect Community2mo ago
4 replies
gavriguy

Mocking layers when testing Effect Atom

Hi, How would you approach testing Effect Atoms that are dependant on Effect services?

For example here is the code from the https://github.com/tim-smart/effect-atom repo:

import { Atom } from "@effect-atom/atom-react"
import { Effect } from "effect"

class Users extends Effect.Service<Users>()("app/Users", {
  effect: Effect.gen(function* () {
    const getAll = Effect.succeed([
      { id: "1", name: "Alice" },
      { id: "2", name: "Bob" },
      { id: "3", name: "Charlie" },
    ])
    return { getAll } as const
  }),
}) {}

// Create a `AtomRuntime` from a `Layer`.
//
//         ┌─── Atom.AtomRuntime<Users>
//         ▼
const runtimeAtom = Atom.runtime(Users.Default)

// You can then use the `AtomRuntime` to make Atoms that use the services from the `Layer`.
const usersAtom = runtimeAtom.atom(
  Effect.gen(function* () {
    const users = yield* Users
    return yield* users.getAll
  }),
)


What you I need to do if I want to test a component that uses the usersAtom? How can I replace the runtime Users.Default layer with a mocked layer on the runtimeAtom in my test without changing the tested component code?
GitHub
Contribute to tim-smart/effect-atom development by creating an account on GitHub.
GitHub - tim-smart/effect-atom
Was this page helpful?