Skip to content

Commit

Permalink
implemented + test for useVal
Browse files Browse the repository at this point in the history
  • Loading branch information
mweststrate committed Dec 16, 2018
1 parent ac6213e commit 186912c
Show file tree
Hide file tree
Showing 8 changed files with 129 additions and 15 deletions.
12 changes: 7 additions & 5 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,13 @@
"request": "launch",
"name": "debug unit test",
"stopOnEntry": false,
"program": "${workspaceRoot}\\node_modules\\jest-cli\\bin\\jest.js",
"args": ["--verbose", "--runInBand", "--config", "jest.config.json", "-i", "${fileBasename}"],
// "windows": {
// "args": ["--verbose", "--runInBand", "--config", "jest.config.json"]
// },
"program": "${workspaceRoot}/node_modules/jest-cli/bin/jest.js",
"args": ["--verbose", "--runInBand", "--config", "jest.config.json", "-i", "${fileBasename}"],
"windows": {
"program": "${workspaceRoot}\\node_modules\\jest-cli\\bin\\jest.js"

// "args": ["--verbose", "--runInBand", "--config", "jest.config.json"]
},
"runtimeArgs": ["--nolazy"]
},
{
Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# RVal


[![npm](https://img.shields.io/npm/v/rval.svg)](https://www.npmjs.com/package/rval) [![size](http://img.badgesize.io/https://cdn.jsdelivr.net/npm/rval/core/index.module.js?compression=gzip)](http://img.badgesize.io/https://cdn.jsdelivr.net/npm/rval/core/index.module.js) [![install size](https://packagephobia.now.sh/badge?p=rval)](https://packagephobia.now.sh/result?p=rval) [![Build Status](https://travis-ci.org/mweststrate/rval.svg?branch=master)](https://travis-ci.org/mweststrate/rval) [![Coverage Status](https://coveralls.io/repos/github/mweststrate/rval/badge.svg?branch=master)](https://coveralls.io/github/mweststrate/rval?branch=master) [![code style: prettier](https://img.shields.io/badge/code_style-prettier-ff69b4.svg)](https://github.com/prettier/prettier) [![Donate](https://img.shields.io/badge/Donate-PayPal-green.svg)](https://www.paypal.me/michelweststrate)
[![npm](https://img.shields.io/npm/v/rval.svg)](https://www.npmjs.com/package/rval) [![size](http://img.badgesize.io/https://unpkg.com/rval/core/index.module.js?compression=gzip)](http://img.badgesize.io/https://unpkg.com/rval/core/index.module.js) [![install size](https://packagephobia.now.sh/badge?p=rval)](https://packagephobia.now.sh/result?p=rval) [![Build Status](https://travis-ci.org/mweststrate/rval.svg?branch=master)](https://travis-ci.org/mweststrate/rval) [![Coverage Status](https://coveralls.io/repos/github/mweststrate/rval/badge.svg?branch=master)](https://coveralls.io/github/mweststrate/rval?branch=master) [![code style: prettier](https://img.shields.io/badge/code_style-prettier-ff69b4.svg)](https://github.com/prettier/prettier) [![Donate](https://img.shields.io/badge/Donate-PayPal-green.svg)](https://www.paypal.me/michelweststrate)

TODO: publish docz docs somewhere and link

Expand Down Expand Up @@ -143,6 +143,7 @@ Todo:

Later
* [ ] support name option
* [ ] abstraction for creating drv / vals and subscribing in hook based component automatically?
* [ ] setter for `drv`?
* [ ] MobX global state compatibility?
* [ ] Docs with docusaurus?
4 changes: 2 additions & 2 deletions examples/boxes/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
},
"dependencies": {
"node-uuid": "^1.4.8",
"react": "^16.7.0-alpha.0",
"react-dom": "^16.7.0-alpha.0",
"react": "^16.7.0-alpha.2",
"react-dom": "^16.7.0-alpha.2",
"react-draggable": "^3.0.5"
},
"scripts": {
Expand Down
5 changes: 5 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,18 @@
"devDependencies": {
"@types/jest": "^23.3.9",
"@types/node": "^10.12.10",
"@types/react": "^16.7.17",
"@types/react-dom": "^16.0.11",
"coveralls": "^3.0.2",
"cross-env": "^5.2.0",
"docz": "^0.12.17",
"docz-theme-default": "^0.12.17",
"immer": "^1.8.0",
"jest": "^23.6.0",
"prettier": "^1.15.2",
"react": "^16.7.0-alpha.2",
"react-dom": "^16.7.0-alpha.2",
"react-testing-library": "^5.4.0",
"rimraf": "^2.6.2",
"rollup": "^0.67.4",
"rollup-plugin-filesize": "^5.0.1",
Expand Down
19 changes: 14 additions & 5 deletions src/rval-react.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,25 @@
import { } from "rval"
import { Observable, sub, isVal, isDrv } from "rval"
import { useState, useEffect } from "react"

// TODO: how to use correct context?

// TODO: explicit subscription is `useVal` arg 1 is a Drv / Val
export function useVal() {

export function useVal<T>(observable: Observable<T>): T {
if (!isVal(observable) && !isDrv(observable)) throw new Error("useval - expected val or drv")
const [val, updater] = useState(observable)
useEffect(() => {
const disposer = sub(observable, updater)
// observable has changed before effect was run first time, so trigger additional update
if (observable() !== val)
updater(observable())
return disposer
}, [observable])
return val
}

export function useDrv() {

}

// TODO: how to use correct context?
export function render() {

}
21 changes: 21 additions & 0 deletions tests/react.spec.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { val } from 'rval'
import { useVal } from 'rval/react'
import * as React from 'react'
import { render, waitForElement } from 'react-testing-library'

test('useVal - 1 ', async () => {
const counter = val(0)
const Comp = () => {
const c = useVal(counter)
return <h1>{c}</h1>
}

const re = render(<Comp />)
expect(re.container.innerHTML).toEqual('<h1>0</h1>')

counter(counter() + 1)
await waitForElement(() => re.container.innerHTML === '<h1>1</h1>')

counter(counter() + 1)
await waitForElement(() => re.container.innerHTML === '<h1>2</h1>')
})
1 change: 1 addition & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
// "allowJs": true, /* Allow javascript files to be compiled. */
// "checkJs": true, /* Report errors in .js files. */
// "jsx": "preserve", /* Specify JSX code generation: 'preserve', 'react-native', or 'react'. */
"jsx": "react",
"declaration": true /* Generates corresponding '.d.ts' file. */,
// "declarationMap": true, /* Generates a sourcemap for each corresponding '.d.ts' file. */
// "sourceMap": true, /* Generates corresponding '.map' file. */
Expand Down
79 changes: 77 additions & 2 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -706,7 +706,7 @@
"@babel/helper-plugin-utils" "^7.0.0"
"@babel/plugin-transform-typescript" "^7.1.0"

"@babel/runtime@^7.0.0", "@babel/runtime@^7.2.0":
"@babel/runtime@^7.0.0", "@babel/runtime@^7.1.5", "@babel/runtime@^7.2.0":
version "7.2.0"
resolved "https://registry.yarnpkg.com/@babel/runtime/-/runtime-7.2.0.tgz#b03e42eeddf5898e00646e4c840fa07ba8dcad7f"
integrity sha512-oouEibCbHMVdZSDlJBO6bZmID/zA/G/Qx3H1d3rSNPTD+L8UNKvCat7aKWSJ74zYbm5zWGh0GQN0hKj8zYFTCg==
Expand Down Expand Up @@ -901,6 +901,11 @@
resolved "https://registry.yarnpkg.com/@nodelib/fs.stat/-/fs.stat-1.1.3.tgz#2b5a3ab3f918cca48a8c754c08168e3f03eba61b"
integrity sha512-shAmDyaQC4H92APFoIaVDHCx5bStIocgvbwQyxPRrbUY20V1EYTbSDchWbuwlMG3V17cprZhA6+78JfB+3DTPw==

"@sheerun/mutationobserver-shim@^0.3.2":
version "0.3.2"
resolved "https://registry.yarnpkg.com/@sheerun/mutationobserver-shim/-/mutationobserver-shim-0.3.2.tgz#8013f2af54a2b7d735f71560ff360d3a8176a87b"
integrity sha512-vTCdPp/T/Q3oSqwHmZ5Kpa9oI7iLtGl3RQaA/NyLHikvcrPxACkkKVr/XzkSPJWXHRhKGzVvb0urJsbMlRxi1Q==

"@shellscape/koa-send@^4.1.0":
version "4.1.3"
resolved "https://registry.yarnpkg.com/@shellscape/koa-send/-/koa-send-4.1.3.tgz#1a7c8df21f63487e060b7bfd8ed82e1d3c4ae0b0"
Expand Down Expand Up @@ -1057,11 +1062,31 @@
resolved "https://registry.yarnpkg.com/@types/node/-/node-10.12.10.tgz#4fa76e6598b7de3f0cb6ec3abacc4f59e5b3a2ce"
integrity sha512-8xZEYckCbUVgK8Eg7lf5Iy4COKJ5uXlnIOnePN0WUwSQggy9tolM+tDJf7wMOnT/JT/W9xDYIaYggt3mRV2O5w==

"@types/prop-types@*":
version "15.5.8"
resolved "https://registry.yarnpkg.com/@types/prop-types/-/prop-types-15.5.8.tgz#8ae4e0ea205fe95c3901a5a1df7f66495e3a56ce"
integrity sha512-3AQoUxQcQtLHsK25wtTWIoIpgYjH3vSDroZOUr7PpCHw/jLY1RB9z9E8dBT/OSmwStVgkRNvdh+ZHNiomRieaw==

"@types/q@^1.5.1":
version "1.5.1"
resolved "https://registry.yarnpkg.com/@types/q/-/q-1.5.1.tgz#48fd98c1561fe718b61733daed46ff115b496e18"
integrity sha512-eqz8c/0kwNi/OEHQfvIuJVLTst3in0e7uTKeuY+WL/zfKn0xVujOTp42bS/vUUokhK5P2BppLd9JXMOMHcgbjA==

"@types/react-dom@^16.0.11":
version "16.0.11"
resolved "https://registry.yarnpkg.com/@types/react-dom/-/react-dom-16.0.11.tgz#bd10ccb0d9260343f4b9a49d4f7a8330a5c1f081"
integrity sha512-x6zUx9/42B5Kl2Vl9HlopV8JF64wLpX3c+Pst9kc1HgzrsH+mkehe/zmHMQTplIrR48H2gpU7ZqurQolYu8XBA==
dependencies:
"@types/react" "*"

"@types/react@*", "@types/react@^16.7.17":
version "16.7.17"
resolved "https://registry.yarnpkg.com/@types/react/-/react-16.7.17.tgz#3242e796a1ffbba4f49eae5915a67f4c079504e9"
integrity sha512-YcXcaoXaxo7A76mBCGlKlN2aZu3REQfF0DTrhiyXVJLA7PDdxVCr+wiQOrkVNn44D/zLlIyDSn3U918Ve0AaEA==
dependencies:
"@types/prop-types" "*"
csstype "^2.2.0"

"@types/unist@*", "@types/unist@^2.0.0", "@types/unist@^2.0.2":
version "2.0.2"
resolved "https://registry.yarnpkg.com/@types/unist/-/unist-2.0.2.tgz#5dc0a7f76809b7518c0df58689cd16a19bd751c6"
Expand Down Expand Up @@ -3111,7 +3136,7 @@ cssstyle@^1.0.0:
dependencies:
cssom "0.3.x"

csstype@^2.5.7:
csstype@^2.2.0, csstype@^2.5.7:
version "2.5.8"
resolved "https://registry.yarnpkg.com/csstype/-/csstype-2.5.8.tgz#4ce5aa16ea0d562ef9105fa3ae2676f199586a35"
integrity sha512-r4DbsyNJ7slwBSKoGesxDubRWJ71ghG8W2+1HcsDlAo12KGca9dDLv0u98tfdFw7ldBdoA7XmCnI6Q8LpAJXaQ==
Expand Down Expand Up @@ -3584,6 +3609,16 @@ dom-serializer@0:
domelementtype "~1.1.1"
entities "~1.1.1"

dom-testing-library@^3.13.1:
version "3.16.0"
resolved "https://registry.yarnpkg.com/dom-testing-library/-/dom-testing-library-3.16.0.tgz#f2bfcdba1c077146ab4058099767b2f49014ff3a"
integrity sha512-KfhmOHA0R78V37rcFvfWYfgCXu3FaOgrUVci1Zmu7JwlPBBifgbHF/Epdifay4mYQLTCpN9tCzdGgZAHOuL4QA==
dependencies:
"@babel/runtime" "^7.1.5"
"@sheerun/mutationobserver-shim" "^0.3.2"
pretty-format "^23.6.0"
wait-for-expect "^1.1.0"

dom-walk@^0.1.0:
version "0.1.1"
resolved "https://registry.yarnpkg.com/dom-walk/-/dom-walk-0.1.1.tgz#672226dc74c8f799ad35307df936aba11acd6018"
Expand Down Expand Up @@ -8193,6 +8228,16 @@ react-dom@^16.6.3:
prop-types "^15.6.2"
scheduler "^0.11.2"

react-dom@^16.7.0-alpha.2:
version "16.7.0-alpha.2"
resolved "https://registry.yarnpkg.com/react-dom/-/react-dom-16.7.0-alpha.2.tgz#16632880ed43676315991d8b412cce6975a30282"
integrity sha512-o0mMw8jBlwHjGZEy/vvKd/6giAX0+skREMOTs3/QHmgi+yAhUClp4My4Z9lsKy3SXV+03uPdm1l/QM7NTcGuMw==
dependencies:
loose-envify "^1.1.0"
object-assign "^4.1.1"
prop-types "^15.6.2"
scheduler "^0.12.0-alpha.2"

react-error-overlay@^5.1.0:
version "5.1.0"
resolved "https://registry.yarnpkg.com/react-error-overlay/-/react-error-overlay-5.1.0.tgz#c516995a5652e7bfbed8b497910d5280df74a7e8"
Expand Down Expand Up @@ -8301,6 +8346,13 @@ react-sizes@^1.0.4:
lodash.throttle "^4.1.1"
prop-types "^15.6.0"

react-testing-library@^5.4.0:
version "5.4.0"
resolved "https://registry.yarnpkg.com/react-testing-library/-/react-testing-library-5.4.0.tgz#dd3b1ee6f49d6c01daa9435bfac35390ebc03c25"
integrity sha512-d/z7MNWUmZNKMlnvBJ0MeRmNGckrtJyvgMMQJGZJdX1zLc6v5j/+SeaGO0x/30duFGTe14VgsxbaUYc9UPf8Pw==
dependencies:
dom-testing-library "^3.13.1"

react@^16.6.3:
version "16.6.3"
resolved "https://registry.yarnpkg.com/react/-/react-16.6.3.tgz#25d77c91911d6bbdd23db41e70fb094cc1e0871c"
Expand All @@ -8311,6 +8363,16 @@ react@^16.6.3:
prop-types "^15.6.2"
scheduler "^0.11.2"

react@^16.7.0-alpha.2:
version "16.7.0-alpha.2"
resolved "https://registry.yarnpkg.com/react/-/react-16.7.0-alpha.2.tgz#924f2ae843a46ea82d104a8def7a599fbf2c78ce"
integrity sha512-Xh1CC8KkqIojhC+LFXd21jxlVtzoVYdGnQAi/I2+dxbmos9ghbx5TQf9/nDxc4WxaFfUQJkya0w1k6rMeyIaxQ==
dependencies:
loose-envify "^1.1.0"
object-assign "^4.1.1"
prop-types "^15.6.2"
scheduler "^0.12.0-alpha.2"

read-pkg-up@^1.0.1:
version "1.0.1"
resolved "https://registry.yarnpkg.com/read-pkg-up/-/read-pkg-up-1.0.1.tgz#9d63c13276c065918d57f002a57f40a1b643fb02"
Expand Down Expand Up @@ -8924,6 +8986,14 @@ scheduler@^0.11.2:
loose-envify "^1.1.0"
object-assign "^4.1.1"

scheduler@^0.12.0-alpha.2:
version "0.12.0-alpha.3"
resolved "https://registry.yarnpkg.com/scheduler/-/scheduler-0.12.0-alpha.3.tgz#59afcaba1cb79e3e8bee91de94eb8f42c9152c2b"
integrity sha512-KADuBlOWSrT/DCt/oA+NgsNamRCsfz7wj+leaeGjGHipNClsqhjOPogKkJgem6WLAv/QzxW8bE7zlGc9OxiYSQ==
dependencies:
loose-envify "^1.1.0"
object-assign "^4.1.1"

schema-utils@^0.4.2, schema-utils@^0.4.4:
version "0.4.7"
resolved "https://registry.yarnpkg.com/schema-utils/-/schema-utils-0.4.7.tgz#ba74f597d2be2ea880131746ee17d0a093c68187"
Expand Down Expand Up @@ -10335,6 +10405,11 @@ w3c-hr-time@^1.0.1:
dependencies:
browser-process-hrtime "^0.1.2"

wait-for-expect@^1.1.0:
version "1.1.0"
resolved "https://registry.yarnpkg.com/wait-for-expect/-/wait-for-expect-1.1.0.tgz#6607375c3f79d32add35cd2c87ce13f351a3d453"
integrity sha512-vQDokqxyMyknfX3luCDn16bSaRcOyH6gGuUXMIbxBLeTo6nWuEWYqMTT9a+44FmW8c2m6TRWBdNvBBjA1hwEKg==

walker@~1.0.5:
version "1.0.7"
resolved "https://registry.yarnpkg.com/walker/-/walker-1.0.7.tgz#2f7f9b8fd10d677262b18a884e28d19618e028fb"
Expand Down

0 comments on commit 186912c

Please sign in to comment.