Skip to content

Commit

Permalink
chore(tests/utils): add observable tests (#2008)
Browse files Browse the repository at this point in the history
* observable-tests

* observable-tests: plain rxjs with Subject

* observable-tests: comments

* Update atomWithObservable.test.tsx

---------

Co-authored-by: Daishi Kato <dai-shi@users.noreply.github.com>
  • Loading branch information
dakom and dai-shi authored Sep 2, 2023
1 parent dbfa6e4 commit ccba918
Showing 1 changed file with 30 additions and 1 deletion.
31 changes: 30 additions & 1 deletion tests/react/vanilla-utils/atomWithObservable.test.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Component, StrictMode, Suspense, useState } from 'react'
import type { ReactElement, ReactNode } from 'react'
import { act, fireEvent, render, waitFor } from '@testing-library/react'
import { BehaviorSubject, Observable, Subject, delay, of } from 'rxjs'
import { BehaviorSubject, Observable, Subject, delay, map, of } from 'rxjs'
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
import { fromValue, makeSubject, pipe, toObservable } from 'wonka'
import { useAtom, useAtomValue, useSetAtom } from 'jotai/react'
Expand Down Expand Up @@ -795,4 +795,33 @@ describe('atomWithObservable vanilla tests', () => {

unsub()
})

it('can propagate updates with rxjs chains', async () => {
const store = createStore()

const single$ = new Subject<number>()
const double$ = single$.pipe(map((n) => n * 2))

const singleAtom = atomWithObservable(() => single$)
const doubleAtom = atomWithObservable(() => double$)

const unsubs = [
store.sub(singleAtom, () => {}),
store.sub(doubleAtom, () => {}),
]

single$.next(1)
expect(store.get(singleAtom)).toBe(1)
expect(store.get(doubleAtom)).toBe(2)

single$.next(2)
expect(store.get(singleAtom)).toBe(2)
expect(store.get(doubleAtom)).toBe(4)

single$.next(3)
expect(store.get(singleAtom)).toBe(3)
expect(store.get(doubleAtom)).toBe(6)

unsubs.forEach((unsub) => unsub())
})
})

1 comment on commit ccba918

@vercel
Copy link

@vercel vercel bot commented on ccba918 Sep 2, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.