Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(reactify)!: enable support for reactive getter by default (#1860)
- Loading branch information
Showing
3 changed files
with
88 additions
and
69 deletions.
There are no files selected for viewing
This file contains 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 |
---|---|---|
@@ -1,92 +1,96 @@ | ||
import { ref } from 'vue-demi' | ||
import { useSetup } from '../../.test' | ||
import { reactify } from '.' | ||
|
||
describe('reactify', () => { | ||
it('one arg', () => { | ||
useSetup(() => { | ||
const base = ref(1.5) | ||
const floor = reactify(Math.floor) | ||
const result = floor(base) | ||
const base = ref(1.5) | ||
const floor = reactify(Math.floor) | ||
const result = floor(base) | ||
|
||
expect(result.value).toBe(1) | ||
expect(result.value).toBe(1) | ||
|
||
base.value = 2.8 | ||
base.value = 2.8 | ||
|
||
expect(result.value).toBe(2) | ||
}) | ||
expect(result.value).toBe(2) | ||
}) | ||
|
||
it('two args', () => { | ||
useSetup(() => { | ||
const base = ref(0) | ||
const exponent = ref(0) | ||
const pow = reactify(Math.pow) | ||
const result = pow(base, exponent) | ||
const base = ref(0) | ||
const exponent = ref(0) | ||
const pow = reactify(Math.pow) | ||
const result = pow(base, exponent) | ||
|
||
expect(base.value).toBe(0) | ||
expect(result.value).toBe(1) | ||
expect(base.value).toBe(0) | ||
expect(result.value).toBe(1) | ||
|
||
base.value = 2 | ||
exponent.value = 2 | ||
base.value = 2 | ||
exponent.value = 2 | ||
|
||
expect(result.value).toBe(4) | ||
expect(result.value).toBe(4) | ||
|
||
base.value = 3 | ||
base.value = 3 | ||
|
||
expect(result.value).toBe(9) | ||
expect(result.value).toBe(9) | ||
|
||
exponent.value = 3 | ||
exponent.value = 3 | ||
|
||
expect(result.value).toBe(27) | ||
}) | ||
expect(result.value).toBe(27) | ||
}) | ||
|
||
it('mixed with literal', () => { | ||
useSetup(() => { | ||
const base = ref(0) | ||
const pow = reactify(Math.pow) | ||
const result = pow(base, 2) | ||
const base = ref(0) | ||
const pow = reactify(Math.pow) | ||
const result = pow(base, 2) | ||
|
||
expect(base.value).toBe(0) | ||
expect(result.value).toBe(0) | ||
expect(base.value).toBe(0) | ||
expect(result.value).toBe(0) | ||
|
||
base.value = 10 | ||
expect(result.value).toBe(100) | ||
}) | ||
base.value = 10 | ||
expect(result.value).toBe(100) | ||
}) | ||
|
||
it('JSON.stringify', () => { | ||
useSetup(() => { | ||
const base = ref<any>(0) | ||
const stringify = reactify(JSON.stringify) | ||
const result = stringify(base, null, 2) | ||
const base = ref<any>(0) | ||
const stringify = reactify(JSON.stringify) | ||
const result = stringify(base, null, 2) | ||
|
||
expect(base.value).toBe(0) | ||
expect(result.value).toBe('0') | ||
expect(base.value).toBe(0) | ||
expect(result.value).toBe('0') | ||
|
||
base.value = { foo: 'bar' } | ||
expect(result.value).toBe('{\n "foo": "bar"\n}') | ||
}) | ||
base.value = { foo: 'bar' } | ||
expect(result.value).toBe('{\n "foo": "bar"\n}') | ||
}) | ||
|
||
it('Pythagorean theorem', () => { | ||
useSetup(() => { | ||
const pow = reactify(Math.pow) | ||
const sqrt = reactify(Math.sqrt) | ||
const add = reactify((a: number, b: number) => a + b) | ||
const pow = reactify(Math.pow) | ||
const sqrt = reactify(Math.sqrt) | ||
const add = reactify((a: number, b: number) => a + b) | ||
|
||
const a = ref(3) | ||
const b = ref(4) | ||
const a = ref(3) | ||
const b = ref(4) | ||
|
||
const c = sqrt(add(pow(a, 2), pow(b, 2))) | ||
const c = sqrt(add(pow(a, 2), pow(b, 2))) | ||
|
||
expect(c.value).toBe(5) | ||
expect(c.value).toBe(5) | ||
|
||
a.value = 5 | ||
b.value = 12 | ||
a.value = 5 | ||
b.value = 12 | ||
|
||
expect(c.value).toBe(13) | ||
}) | ||
expect(c.value).toBe(13) | ||
}) | ||
|
||
it('computed getter', () => { | ||
const add = reactify((a: number, b: number) => a + b) | ||
|
||
const a = ref(3) | ||
const b = ref(4) | ||
|
||
const c = add(a, () => b.value) | ||
|
||
expect(c.value).toBe(7) | ||
|
||
b.value = 12 | ||
|
||
expect(c.value).toBe(15) | ||
}) | ||
}) |
This file contains 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 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