Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(signals): signals testing package with provideMockSignalStore #4252

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion modules/signals/rxjs-interop/src/rx-method.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ import { isObservable, noop, Observable, Subject, Unsubscribable } from 'rxjs';

type RxMethodInput<Input> = Input | Observable<Input> | Signal<Input>;

type RxMethod<Input> = ((input: RxMethodInput<Input>) => Unsubscribable) &
export type RxMethod<Input> = ((
input: RxMethodInput<Input>
) => Unsubscribable) &
Unsubscribable;

export function rxMethod<Input>(
Expand Down
24 changes: 24 additions & 0 deletions modules/signals/testing/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* Public API Surface of fake-rx-method
*/

export {
FakeRxMethod,
FAKE_RX_METHOD,
newFakeRxMethod,
asFakeRxMethod,
getRxMethodFake,
} from './src/fake-rx-method';

/*
* Public API Surface of mock-signal-store
*/

export {
MockSignalStore,
ProvideMockSignalStoreParams,
provideMockSignalStore,
UnwrapProvider,
asMockSignalStore,
asSinonSpy,
} from './src/mock-signal-store';
5 changes: 5 additions & 0 deletions modules/signals/testing/ng-package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"lib": {
"entryFile": "index.ts"
}
}
85 changes: 85 additions & 0 deletions modules/signals/testing/spec/fake-rx-method.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
import { ChangeDetectionStrategy, Component, signal } from '@angular/core';
import { ComponentFixture, TestBed } from '@angular/core/testing';
import {
FakeRxMethod,
asFakeRxMethod,
getRxMethodFake,
newFakeRxMethod,
} from '../src/fake-rx-method';
import { Subject } from 'rxjs';
import { RxMethod } from 'modules/signals/rxjs-interop/src/rx-method';

@Component({
selector: 'app-test',
standalone: true,
changeDetection: ChangeDetectionStrategy.OnPush,
template: ``,
})
export class TestComponent {
fakeRxMethod = newFakeRxMethod<number>();
}

describe('FakeRxMethod', () => {
describe('newFakeRxMethod and getRxMethodFake', () => {
let component: TestComponent;
let fixture: ComponentFixture<TestComponent>;
let fakeRxMethod: FakeRxMethod<number>;

beforeEach(() => {
TestBed.configureTestingModule({
imports: [TestComponent],
}).compileComponents();

fixture = TestBed.createComponent(TestComponent);
component = fixture.componentInstance;
fakeRxMethod = component.fakeRxMethod;
});

it('updates the Sinon fake on imperative calls', () => {
fakeRxMethod(11);
expect(getRxMethodFake(fakeRxMethod).callCount).toBe(1);
expect(getRxMethodFake(fakeRxMethod).lastCall.args).toEqual([11]);
});

it('updates the Sinon fake when an observable input emits', () => {
const o = new Subject<number>();
fakeRxMethod(o);
o.next(11);
expect(getRxMethodFake(fakeRxMethod).callCount).toBe(1);
expect(getRxMethodFake(fakeRxMethod).lastCall.args).toEqual([11]);
o.next(22);
expect(getRxMethodFake(fakeRxMethod).callCount).toBe(2);
expect(getRxMethodFake(fakeRxMethod).lastCall.args).toEqual([22]);
});

it('updates the Sinon fake when a signal input emits (1)', () => {
const s = signal(72);
fakeRxMethod(s);
fixture.detectChanges();
expect(getRxMethodFake(fakeRxMethod).callCount).toBe(1);
expect(getRxMethodFake(fakeRxMethod).lastCall.args).toEqual([72]);
s.set(23);
fixture.detectChanges();
expect(getRxMethodFake(fakeRxMethod).callCount).toBe(2);
expect(getRxMethodFake(fakeRxMethod).lastCall.args).toEqual([23]);
});

it('updates the Sinon fake when a signal input emits (2)', () => {
const s = signal(72);
fakeRxMethod(s);
s.set(23);
fixture.detectChanges();
expect(getRxMethodFake(fakeRxMethod).callCount).toBe(1);
expect(getRxMethodFake(fakeRxMethod).lastCall.args).toEqual([23]);
});
});

describe('asFakeRxMethod', () => {
it('should return the input wihtout change', () => {
TestBed.runInInjectionContext(() => {
const rxMethod = newFakeRxMethod<number>() as RxMethod<number>;
expect(asFakeRxMethod(rxMethod)).toEqual(rxMethod);
});
});
});
});
Loading