-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
516 additions
and
4 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
178 changes: 178 additions & 0 deletions
178
apps/common-app/src/examples/RuntimeTests/tests/plugin/contextObjects.test.tsx
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 |
---|---|---|
@@ -0,0 +1,178 @@ | ||
import React, { useEffect } from 'react'; | ||
import { View } from 'react-native'; | ||
import { useSharedValue, runOnUI } from 'react-native-reanimated'; | ||
import { | ||
render, | ||
wait, | ||
describe, | ||
getRegisteredValue, | ||
registerValue, | ||
test, | ||
expect, | ||
} from '../../ReanimatedRuntimeTestsRunner/RuntimeTestsApi'; | ||
|
||
const SHARED_VALUE_REF = 'SHARED_VALUE_REF'; | ||
|
||
describe('Test context objects', () => { | ||
test('methods are workletized', async () => { | ||
const ExampleComponent = () => { | ||
const output = useSharedValue<number | null>(null); | ||
registerValue(SHARED_VALUE_REF, output); | ||
const contextObject = { | ||
foo() { | ||
return 1; | ||
}, | ||
__workletObject: true, | ||
}; | ||
|
||
useEffect(() => { | ||
runOnUI(() => { | ||
output.value = contextObject.foo(); | ||
})(); | ||
}); | ||
|
||
return <View />; | ||
}; | ||
await render(<ExampleComponent />); | ||
await wait(100); | ||
const sharedValue = await getRegisteredValue(SHARED_VALUE_REF); | ||
expect(sharedValue.onUI).toBe(1); | ||
}); | ||
|
||
test('properties are workletized', async () => { | ||
const ExampleComponent = () => { | ||
const output = useSharedValue<number | null>(null); | ||
registerValue(SHARED_VALUE_REF, output); | ||
const contextObject = { | ||
foo: () => 1, | ||
__workletObject: true, | ||
}; | ||
|
||
useEffect(() => { | ||
runOnUI(() => { | ||
output.value = contextObject.foo(); | ||
})(); | ||
}); | ||
|
||
return <View />; | ||
}; | ||
await render(<ExampleComponent />); | ||
await wait(100); | ||
const sharedValue = await getRegisteredValue(SHARED_VALUE_REF); | ||
expect(sharedValue.onUI).toBe(1); | ||
}); | ||
|
||
test('methods preserve implicit context', async () => { | ||
const ExampleComponent = () => { | ||
const output = useSharedValue<number | null>(null); | ||
registerValue(SHARED_VALUE_REF, output); | ||
const contextObject = { | ||
foo() { | ||
return 1; | ||
}, | ||
bar() { | ||
return this.foo() + 1; | ||
}, | ||
__workletObject: true, | ||
}; | ||
|
||
useEffect(() => { | ||
runOnUI(() => { | ||
output.value = contextObject.bar(); | ||
})(); | ||
}); | ||
|
||
return <View />; | ||
}; | ||
await render(<ExampleComponent />); | ||
await wait(100); | ||
const sharedValue = await getRegisteredValue(SHARED_VALUE_REF); | ||
expect(sharedValue.onUI).toBe(2); | ||
}); | ||
|
||
test('methods preserve explicit context', async () => { | ||
const ExampleComponent = () => { | ||
const output = useSharedValue<number | null>(null); | ||
registerValue(SHARED_VALUE_REF, output); | ||
const contextObject = { | ||
foo() { | ||
return 1; | ||
}, | ||
bar() { | ||
return this.foo.call(contextObject) + 1; | ||
}, | ||
__workletObject: true, | ||
}; | ||
|
||
useEffect(() => { | ||
runOnUI(() => { | ||
output.value = contextObject.bar(); | ||
})(); | ||
}); | ||
|
||
return <View />; | ||
}; | ||
await render(<ExampleComponent />); | ||
await wait(100); | ||
const sharedValue = await getRegisteredValue(SHARED_VALUE_REF); | ||
expect(sharedValue.onUI).toBe(2); | ||
}); | ||
|
||
test('methods change the state of the object', async () => { | ||
const ExampleComponent = () => { | ||
const output = useSharedValue<number | null>(null); | ||
registerValue(SHARED_VALUE_REF, output); | ||
const contextObject = { | ||
foo: 1, | ||
bar() { | ||
this.foo += 1; | ||
}, | ||
__workletObject: true, | ||
}; | ||
|
||
useEffect(() => { | ||
runOnUI(() => { | ||
contextObject.bar(); | ||
output.value = contextObject.foo; | ||
})(); | ||
}); | ||
|
||
return <View />; | ||
}; | ||
await render(<ExampleComponent />); | ||
await wait(100); | ||
const sharedValue = await getRegisteredValue(SHARED_VALUE_REF); | ||
expect(sharedValue.onUI).toBe(2); | ||
}); | ||
|
||
test("the object doesn't persist in memory", async () => { | ||
const ExampleComponent = () => { | ||
const output = useSharedValue<number | null>(null); | ||
registerValue(SHARED_VALUE_REF, output); | ||
const contextObject = { | ||
foo: 1, | ||
bar() { | ||
this.foo += 1; | ||
}, | ||
__workletObject: true, | ||
}; | ||
|
||
useEffect(() => { | ||
runOnUI(() => { | ||
contextObject.bar(); | ||
output.value = contextObject.foo; | ||
})(); | ||
}); | ||
|
||
return <View />; | ||
}; | ||
await render(<ExampleComponent />); | ||
await wait(100); | ||
const sharedValue = await getRegisteredValue(SHARED_VALUE_REF); | ||
expect(sharedValue.onUI).toBe(2); | ||
await render(<ExampleComponent />); | ||
await wait(100); | ||
const sharedValue2 = await getRegisteredValue(SHARED_VALUE_REF); | ||
expect(sharedValue2.onUI).toBe(2); | ||
}); | ||
}); |
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
Oops, something went wrong.