-
-
Notifications
You must be signed in to change notification settings - Fork 5
feat: useToggle #55
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
feat: useToggle #55
Changes from all commits
Commits
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': minor | ||
'unuse-angular': minor | ||
'unuse-react': minor | ||
'unuse-solid': minor | ||
'unuse-vue': minor | ||
--- | ||
|
||
feat: useToggle |
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,126 @@ | ||
// @vitest-environment happy-dom | ||
|
||
import type { WritableSignal } from '@angular/core'; | ||
import { isSignal, signal } from '@angular/core'; | ||
import { getTestBed } from '@angular/core/testing'; | ||
import { | ||
BrowserTestingModule, | ||
platformBrowserTesting, | ||
} from '@angular/platform-browser/testing'; | ||
import { beforeAll, expect, it } from 'vitest'; | ||
import { describeAngular, ZonelessTestModule } from '../_testUtils/angular'; | ||
import type { ToggleFn } from './index'; | ||
import { useToggle } from './index'; | ||
|
||
describeAngular('useToggle', () => { | ||
beforeAll(() => { | ||
getTestBed().initTestEnvironment( | ||
[BrowserTestingModule, ZonelessTestModule], | ||
platformBrowserTesting() | ||
); | ||
}); | ||
|
||
it('default result', () => { | ||
const result = useToggle() as unknown as [ | ||
WritableSignal<boolean>, | ||
ToggleFn, | ||
]; | ||
const [value, toggle] = result; | ||
|
||
expect(Array.isArray(result)).toBe(true); | ||
expect(result).toHaveLength(2); | ||
|
||
expect(toggle).toBeTypeOf('function'); | ||
expect(value).toSatisfy(isSignal); | ||
expect(value()).toBe(false); | ||
}); | ||
|
||
it('default result with initialValue', () => { | ||
const result = useToggle(true) as unknown as [ | ||
WritableSignal<boolean>, | ||
ToggleFn, | ||
]; | ||
const [value, toggle] = result; | ||
|
||
expect(Array.isArray(result)).toBe(true); | ||
expect(result).toHaveLength(2); | ||
|
||
expect(toggle).toBeTypeOf('function'); | ||
expect(value).toSatisfy(isSignal); | ||
expect(value()).toBe(true); | ||
}); | ||
|
||
it('should toggle', () => { | ||
const result = useToggle() as unknown as [ | ||
WritableSignal<boolean>, | ||
ToggleFn, | ||
]; | ||
const [value, toggle] = result; | ||
|
||
expect(toggle()).toBe(true); | ||
expect(value()).toBe(true); | ||
|
||
expect(toggle()).toBe(false); | ||
expect(value()).toBe(false); | ||
}); | ||
|
||
it('should receive toggle param', () => { | ||
const result = useToggle() as unknown as [ | ||
WritableSignal<boolean>, | ||
ToggleFn, | ||
]; | ||
const [value, toggle] = result; | ||
|
||
expect(toggle(false)).toBe(false); | ||
expect(value()).toBe(false); | ||
|
||
expect(toggle(true)).toBe(true); | ||
expect(value()).toBe(true); | ||
}); | ||
|
||
it('signal initialValue', () => { | ||
getTestBed().runInInjectionContext(() => { | ||
const isDark = signal(true); | ||
const toggle = useToggle(isDark); | ||
|
||
expect(toggle).toBeTypeOf('function'); | ||
|
||
expect(toggle()).toBe(false); | ||
expect(isDark()).toBe(false); | ||
|
||
expect(toggle()).toBe(true); | ||
expect(isDark()).toBe(true); | ||
|
||
expect(toggle(false)).toBe(false); | ||
expect(isDark()).toBe(false); | ||
|
||
expect(toggle(true)).toBe(true); | ||
expect(isDark()).toBe(true); | ||
}); | ||
}); | ||
|
||
it('should toggle with truthy & falsy', () => { | ||
getTestBed().runInInjectionContext(() => { | ||
const status = signal('ON'); | ||
const toggle = useToggle(status, { | ||
truthyValue: 'ON', | ||
falsyValue: 'OFF', | ||
}); | ||
|
||
expect(status()).toBe('ON'); | ||
expect(toggle).toBeTypeOf('function'); | ||
|
||
expect(toggle()).toBe('OFF'); | ||
expect(status()).toBe('OFF'); | ||
|
||
expect(toggle()).toBe('ON'); | ||
expect(status()).toBe('ON'); | ||
|
||
expect(toggle('OFF')).toBe('OFF'); | ||
expect(status()).toBe('OFF'); | ||
|
||
expect(toggle('ON')).toBe('ON'); | ||
expect(status()).toBe('ON'); | ||
}); | ||
}); | ||
}); |
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,122 @@ | ||
// @vitest-environment happy-dom | ||
|
||
import { renderHook } from '@testing-library/react'; | ||
import { useState } from 'react'; | ||
import { expect, it } from 'vitest'; | ||
import { describeReact } from '../_testUtils/react'; | ||
import type { ToggleFn } from './index'; | ||
import { useToggle } from './index'; | ||
|
||
describeReact('useToggle', () => { | ||
it('default result', () => { | ||
renderHook(() => { | ||
const result = useToggle() as unknown as [ | ||
ReturnType<typeof useState<boolean>>, | ||
ToggleFn, | ||
]; | ||
const [value, toggle] = result; | ||
|
||
expect(Array.isArray(result)).toBe(true); | ||
expect(result).toHaveLength(2); | ||
|
||
expect(toggle).toBeTypeOf('function'); | ||
expect(value).toSatisfy(Array.isArray); | ||
expect(value[0]).toBe(false); | ||
}); | ||
}); | ||
|
||
it('default result with initialValue', () => { | ||
renderHook(() => { | ||
const result = useToggle(true) as unknown as [ | ||
ReturnType<typeof useState<boolean>>, | ||
ToggleFn, | ||
]; | ||
const [value, toggle] = result; | ||
|
||
expect(Array.isArray(result)).toBe(true); | ||
expect(result).toHaveLength(2); | ||
|
||
expect(toggle).toBeTypeOf('function'); | ||
expect(value).toBeTypeOf('object'); | ||
expect(value[0]).toBe(true); | ||
}); | ||
}); | ||
|
||
it.todo('should toggle', () => { | ||
renderHook(() => { | ||
const result = useToggle() as unknown as [ | ||
ReturnType<typeof useState<boolean>>, | ||
ToggleFn, | ||
]; | ||
const [value, toggle] = result; | ||
|
||
expect(toggle()).toBe(true); | ||
expect(value[0]).toBe(true); | ||
|
||
expect(toggle()).toBe(false); | ||
expect(value[0]).toBe(false); | ||
}); | ||
}); | ||
Shinigami92 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
it.todo('should receive toggle param', () => { | ||
renderHook(() => { | ||
const result = useToggle() as unknown as [ | ||
ReturnType<typeof useState<boolean>>, | ||
ToggleFn, | ||
]; | ||
const [value, toggle] = result; | ||
|
||
expect(toggle(false)).toBe(false); | ||
expect(value[0]).toBe(false); | ||
|
||
expect(toggle(true)).toBe(true); | ||
expect(value[0]).toBe(true); | ||
}); | ||
}); | ||
|
||
it.todo('state initialValue', () => { | ||
renderHook(() => { | ||
const isDark = useState(true); | ||
const toggle = useToggle(isDark); | ||
|
||
expect(toggle).toBeTypeOf('function'); | ||
|
||
expect(toggle()).toBe(false); | ||
expect(isDark[0]).toBe(false); | ||
|
||
expect(toggle()).toBe(true); | ||
expect(isDark[0]).toBe(true); | ||
|
||
expect(toggle(false)).toBe(false); | ||
expect(isDark[0]).toBe(false); | ||
|
||
expect(toggle(true)).toBe(true); | ||
expect(isDark[0]).toBe(true); | ||
}); | ||
}); | ||
|
||
it.todo('should toggle with truthy & falsy', () => { | ||
renderHook(() => { | ||
const status = useState('ON'); | ||
const toggle = useToggle(status, { | ||
truthyValue: 'ON', | ||
falsyValue: 'OFF', | ||
}); | ||
|
||
expect(status[0]).toBe('ON'); | ||
expect(toggle).toBeTypeOf('function'); | ||
|
||
expect(toggle()).toBe('OFF'); | ||
expect(status[0]).toBe('OFF'); | ||
|
||
expect(toggle()).toBe('ON'); | ||
expect(status[0]).toBe('ON'); | ||
|
||
expect(toggle('OFF')).toBe('OFF'); | ||
expect(status[0]).toBe('OFF'); | ||
|
||
expect(toggle('ON')).toBe('ON'); | ||
expect(status[0]).toBe('ON'); | ||
}); | ||
}); | ||
Shinigami92 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
}); |
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,96 @@ | ||
import type { Signal } from 'solid-js'; | ||
import { createSignal } from 'solid-js'; | ||
import { expect, it } from 'vitest'; | ||
import { describeSolid } from '../_testUtils/solid'; | ||
import type { ToggleFn } from './index'; | ||
import { useToggle } from './index'; | ||
|
||
describeSolid('useToggle', () => { | ||
it('default result', () => { | ||
const result = useToggle() as unknown as [Signal<boolean>, ToggleFn]; | ||
const [value, toggle] = result; | ||
|
||
expect(Array.isArray(result)).toBe(true); | ||
expect(result).toHaveLength(2); | ||
|
||
expect(toggle).toBeTypeOf('function'); | ||
expect(value).toSatisfy(Array.isArray); | ||
expect(value[0]()).toBe(false); | ||
}); | ||
|
||
it('default result with initialValue', () => { | ||
const result = useToggle(true) as unknown as [Signal<boolean>, ToggleFn]; | ||
const [value, toggle] = result; | ||
|
||
expect(Array.isArray(result)).toBe(true); | ||
expect(result).toHaveLength(2); | ||
|
||
expect(toggle).toBeTypeOf('function'); | ||
expect(value).toSatisfy(Array.isArray); | ||
expect(value[0]()).toBe(true); | ||
}); | ||
|
||
it('should toggle', () => { | ||
const result = useToggle() as unknown as [Signal<boolean>, ToggleFn]; | ||
const [value, toggle] = result; | ||
|
||
expect(toggle()).toBe(true); | ||
expect(value[0]()).toBe(true); | ||
|
||
expect(toggle()).toBe(false); | ||
expect(value[0]()).toBe(false); | ||
}); | ||
|
||
it('should receive toggle param', () => { | ||
const result = useToggle() as unknown as [Signal<boolean>, ToggleFn]; | ||
const [value, toggle] = result; | ||
|
||
expect(toggle(false)).toBe(false); | ||
expect(value[0]()).toBe(false); | ||
|
||
expect(toggle(true)).toBe(true); | ||
expect(value[0]()).toBe(true); | ||
}); | ||
|
||
it('signal initialValue', () => { | ||
const isDark = createSignal(true); | ||
const toggle = useToggle(isDark); | ||
|
||
expect(toggle).toBeTypeOf('function'); | ||
|
||
expect(toggle()).toBe(false); | ||
expect(isDark[0]()).toBe(false); | ||
|
||
expect(toggle()).toBe(true); | ||
expect(isDark[0]()).toBe(true); | ||
|
||
expect(toggle(false)).toBe(false); | ||
expect(isDark[0]()).toBe(false); | ||
|
||
expect(toggle(true)).toBe(true); | ||
expect(isDark[0]()).toBe(true); | ||
}); | ||
|
||
it('should toggle with truthy & falsy', () => { | ||
const status = createSignal('ON'); | ||
const toggle = useToggle(status, { | ||
truthyValue: 'ON', | ||
falsyValue: 'OFF', | ||
}); | ||
|
||
expect(status[0]()).toBe('ON'); | ||
expect(toggle).toBeTypeOf('function'); | ||
|
||
expect(toggle()).toBe('OFF'); | ||
expect(status[0]()).toBe('OFF'); | ||
|
||
expect(toggle()).toBe('ON'); | ||
expect(status[0]()).toBe('ON'); | ||
|
||
expect(toggle('OFF')).toBe('OFF'); | ||
expect(status[0]()).toBe('OFF'); | ||
|
||
expect(toggle('ON')).toBe('ON'); | ||
expect(status[0]()).toBe('ON'); | ||
}); | ||
}); |
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.