-
-
Notifications
You must be signed in to change notification settings - Fork 5
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
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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', () => { | ||
Shinigami92 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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(); | ||
}); | ||
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.