import {jest, describe, it, expect} from 'bun:test'; import {Subject, ReadOnlySubject} from '../../src/observables'; describe(`A ReadOnlySubject`, () => { it(`exposes the Subject value`, () => { const subject = new Subject(`init-value`); const readOnlySubject = new ReadOnlySubject(subject); expect(readOnlySubject.value).toBe(`init-value`); }); it(`updates the ReadOnlySubject value when set`, () => { const subject = new Subject('init-value'); const readOnlySubject = new ReadOnlySubject(subject); subject.value = 'some thing'; expect(readOnlySubject.value).toBe('some thing'); }); it(`notifies subcribers when subscribing`, () => { const subject = new Subject('init-value'); const readOnlySubject = new ReadOnlySubject(subject); const listener = jest.fn() as (value: string) => void; readOnlySubject.subscribe(listener); expect(listener).toHaveBeenCalledWith('init-value'); }); it(`notifies subcribers when value changes`, () => { const subject = new Subject('init-value'); const readOnlySubject = new ReadOnlySubject(subject); const listener = jest.fn() as (value: string) => void; readOnlySubject.subscribe(listener); subject.value = 'some thing'; expect(listener).toHaveBeenCalledWith('some thing'); }); it(`notifies all subcribers when value changes`, () => { const subject = new Subject('init-value'); const readOnlySubject = new ReadOnlySubject(subject); const listener1 = jest.fn() as (value: string) => void; const listener2 = jest.fn() as (value: string) => void; const listener3 = jest.fn() as (value: string) => void; readOnlySubject.subscribe(listener1); readOnlySubject.subscribe(listener2); readOnlySubject.subscribe(listener3); subject.value = 'some thing'; expect(listener1).toHaveBeenCalledWith('some thing'); expect(listener2).toHaveBeenCalledWith('some thing'); expect(listener3).toHaveBeenCalledWith('some thing'); }); });