8000 test: cover framework adapters by Shinigami92 · Pull Request #31 · un-ts/unuse · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

test: cover framework adapters #31

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

Merged
merged 2 commits into from
Jun 25, 2025
Merged
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
9 changes: 9 additions & 0 deletions .changeset/bitter-cameras-give.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
'unuse-angular': patch
'unuse-react': patch
'unuse-solid': patch
'unuse-vue': patch
'unuse': patch
---

fix: isUnRef recognize also unSignal, unComputed and getter functions
9 changes: 9 additions & 0 deletions .changeset/dirty-buttons-open.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
'unuse-angular': patch
'unuse-react': patch
'unuse-solid': patch
'unuse-vue': patch
'unuse': patch
---

fix!: remove internal override functions from public API
3 changes: 3 additions & 0 deletions packages/unuse-angular/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@
"alien-signals": "2.0.5",
"unuse": "workspace:*"
},
"devDependencies": {
"@angular/platform-browser": "20.0.4"
},
"peerDependencies": {
"@angular/core": ">=20"
},
Expand Down
186 changes: 186 additions & 0 deletions packages/unuse-angular/src/index.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,186 @@
// @vitest-environment happy-dom

import {
computed,
NgModule,
provideZonelessChangeDetection,
signal,
} from '@angular/core';
import { getTestBed } from '@angular/core/testing';
import {
BrowserTestingModule,
platformBrowserTesting,
} from '@angular/platform-browser/testing';
import { beforeAll, describe, expect, it, vi } from 'vitest';
import {
isUnComputed,
isUnRef,
isUnSignal,
overrideIsUnRefFn,
overrideToUnSignalFn,
overrideTryOnScopeDisposeFn,
overrideUnResolveFn,
toUnSignal,
tryOnScopeDispose,
unComputed,
unResolve,
unSignal,
} from '.';

@NgModule({ providers: [provideZonelessChangeDetection()] })
// eslint-disable-next-line @typescript-eslint/no-extraneous-class
class ZonelessTestModule {}

describe('unuse-angular', () => {
it('should export correct global variable', () => {
expect(globalThis.__UNUSE_FRAMEWORK__).toBe('angular');
});

beforeAll(() => {
getTestBed().initTestEnvironment(
[BrowserTestingModule, ZonelessTestModule],
platformBrowserTesting()
);
});

describe('toUnSignal', () => {
it('should export toUnSignal function', () => {
expect(toUnSignal).toBeTypeOf('function');
});

it('should convert an Angular Signal to an UnSignal', () => {
const angularSignal = signal(42);
getTestBed().runInInjectionContext(() => {
const mySignal = toUnSignal(angularSignal);
expect(mySignal.get()).toBe(42);

mySignal.set(100);
});

expect(angularSignal()).toBe(100);
});

it('should convert a non-reactive value to an UnSignal', () => {
const mySignal = toUnSignal(42);
expect(mySignal.get()).toBe(42);

mySignal.set(100);
expect(mySignal.get()).toBe(100);
});
});

describe('unResolve', () => {
it('should export unResolve function', () => {
expect(unResolve).toBeTypeOf('function');
});

it('should resolve an UnSignal to an Angular WritableSignal', () => {
const mySignal = unSignal(42);
const resolved = unResolve(mySignal);

expect(resolved()).toBe(42);
resolved.set(100);
expect(mySignal.get()).toBe(100);
});

it('should resolve an UnSignal to an Angular Signal when readonly is true', () => {
const mySignal = unSignal(42);
const resolved = unResolve(mySignal, { readonly: true });

expect(resolved()).toBe(42);
});

it('should resolve an UnSignal to an UnSignal when framework is null', () => {
const mySignal = unSignal(42);
const resolved = unResolve(mySignal, { framework: 'none' });

expect(resolved).toSatisfy(isUnSignal);
expect(resolved.get()).toBe(42);
});

it('should resolve an UnSignal to an UnComputed when framework is null and readonly is true', () => {
const mySignal = unSignal(42);
const resolved = unResolve(mySignal, {
framework: 'none',
readonly: true,
});

expect(resolved).toSatisfy(isUnComputed);
expect(resolved.get()).toBe(42);
});
});

describe('tryOnScopeDispose', () => {
it('should export tryOnScopeDispose function', () => {
expect(tryOnScopeDispose).toBeTypeOf('function');
});

it.todo('should call the provided callback on scope dispose', () => {
const callback = vi.fn();
let isDisposable!: boolean;

getTestBed().runInInjectionContext(() => {
isDisposable = tryOnScopeDispose(callback);

expect(isDisposable).toBe(true);
expect(callback).toHaveBeenCalledTimes(0);
});

expect(callback).toHaveBeenCalledTimes(1);
});

it('should return false if not inside an Angular effect scope', () => {
const callback = vi.fn();
const isDisposable = tryOnScopeDispose(callback);

expect(isDisposable).toBe(false);
expect(callback).toHaveBeenCalledTimes(0);
});
});

describe('isUnRef', () => {
it('should export isUnRef function', () => {
expect(isUnRef).toBeTypeOf('function');
});

it('should return true for Angular signal', () => {
const angularSignal = signal(42);
expect(isUnRef(angularSignal)).toBe(true);
});

it('should return true for Angular computed', () => {
const angularComputed = computed(() => 42);
expect(isUnRef(angularComputed)).toBe(true);
});

it('should return true for UnSignal', () => {
const mySignal = unSignal(42);
expect(isUnRef(mySignal)).toBe(true);
});

it('should return true for UnComputed', () => {
const myComputed = unComputed(() => 42);
expect(isUnRef(myComputed)).toBe(true);
});

it('should return true for Getters', () => {
// eslint-disable-next-line unicorn/consistent-function-scoping
const myGetter = () => 42;
expect(isUnRef(myGetter)).toBe(true);
});

it('should return false for non-UnRef values', () => {
expect(isUnRef(42)).toBe(false);
expect(isUnRef('string')).toBe(false);
expect(isUnRef({})).toBe(false);
expect(isUnRef([])).toBe(false);
});
});

it('should not expose override functions', () => {
expect(overrideToUnSignalFn).toBeUndefined();
expect(overrideUnResolveFn).toBeUndefined();
expect(overrideTryOnScopeDisposeFn).toBeUndefined();
expect(overrideIsUnRefFn).toBeUndefined();
});
});
29 changes: 23 additions & 6 deletions packages/unuse-angular/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ import type {
UnSignal,
} from 'unuse';
import {
isUnComputed,
isUnSignal,
overrideIsUnRefFn,
overrideToUnSignalFn,
overrideTryOnScopeDisposeFn,
Expand Down Expand Up @@ -181,11 +183,13 @@ export type UnRef<T> =
| (() => T);

function isUnRef<T>(value: unknown): value is UnRef<T> {
if (isAngularSignal(value)) {
return true;
}

return false;
return (
isAngularSignal(value) ||
// getter
typeof value === 'function' ||
isUnSignal(value) ||
isUnComputed(value)
);
}

overrideToUnSignalFn(toUnSignal);
Expand All @@ -194,4 +198,17 @@ overrideUnResolveFn(unResolve);
overrideTryOnScopeDisposeFn(tryOnScopeDispose);
overrideIsUnRefFn(isUnRef);

export { isUnRef, toUnSignal, tryOnScopeDispose, unResolve };
// Above we export everything from 'unuse'.
// So here we export some internal functions as undefined to make it inaccessible from the public API.
const UNDEFINED = undefined;

export {
isUnRef,
UNDEFINED as overrideIsUnRefFn,
UNDEFINED as overrideToUnSignalFn,
UNDEFINED as overrideTryOnScopeDisposeFn,
UNDEFINED as overrideUnResolveFn,
toUnSignal,
tryOnScopeDispose,
unResolve,
};
1 change: 1 addition & 0 deletions packages/unuse-react/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
"unuse": "workspace:*"
},
"devDependencies": {
"@testing-library/react": "16.3.0",
"@types/react": "19.1.8"
},
"peerDependencies": {
Expand Down
Loading
Loading
0