-
-
Notifications
You must be signed in to change notification settings - Fork 366
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(reakit-system): Replace
useCompose
by useComposeOptions
on `…
…createHook` (#493)
- Loading branch information
Showing
6 changed files
with
286 additions
and
39 deletions.
There are no files selected for viewing
218 changes: 218 additions & 0 deletions
218
packages/reakit-system/src/__tests__/createHook-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,218 @@ | ||
import * as React from "react"; | ||
import { render } from "@testing-library/react"; | ||
import { createHook } from "../createHook"; | ||
import { SystemProvider } from "../SystemProvider"; | ||
|
||
type Options = { | ||
a: string; | ||
}; | ||
|
||
test("useProps", () => { | ||
const useHook = createHook<Options, React.HTMLAttributes<any>>({ | ||
useProps(options, htmlProps) { | ||
return { | ||
...htmlProps, | ||
"data-a": options.a | ||
}; | ||
} | ||
}); | ||
expect(useHook({ a: "a" }, { id: "a" })).toMatchInlineSnapshot(` | ||
Object { | ||
"data-a": "a", | ||
"id": "a", | ||
} | ||
`); | ||
}); | ||
|
||
test("compose useProps", () => { | ||
const useHook = createHook<Options, React.HTMLAttributes<any>>({ | ||
useProps(options, htmlProps) { | ||
return { | ||
...htmlProps, | ||
"data-a": options.a | ||
}; | ||
} | ||
}); | ||
type Options2 = Options & { | ||
b: string; | ||
}; | ||
const useHook2 = createHook<Options2, React.HTMLAttributes<any>>({ | ||
compose: [useHook], | ||
useProps(options, htmlProps) { | ||
return { | ||
...htmlProps, | ||
"data-b": options.b | ||
}; | ||
} | ||
}); | ||
expect(useHook2({ a: "a", b: "b" }, { id: "a" })).toMatchInlineSnapshot(` | ||
Object { | ||
"data-a": "a", | ||
"data-b": "b", | ||
"id": "a", | ||
} | ||
`); | ||
}); | ||
|
||
test("useOptions", () => { | ||
const useHook = createHook<Options, React.HTMLAttributes<any>>({ | ||
useOptions(options) { | ||
return { | ||
...options, | ||
a: "a" | ||
}; | ||
}, | ||
useProps(options, htmlProps) { | ||
return { | ||
...htmlProps, | ||
id: options.a | ||
}; | ||
} | ||
}); | ||
expect(useHook()).toEqual({ id: "a" }); | ||
}); | ||
|
||
test("compose useOptions", () => { | ||
const useHook = createHook<Options, React.HTMLAttributes<any>>({ | ||
useOptions(options) { | ||
return { | ||
...options, | ||
a: `${options.a}b` | ||
}; | ||
} | ||
}); | ||
const useHook2 = createHook<Options, React.HTMLAttributes<any>>({ | ||
compose: [useHook], | ||
useOptions(options) { | ||
return { | ||
...options, | ||
a: "a" | ||
}; | ||
}, | ||
useProps(options, htmlProps) { | ||
return { | ||
...htmlProps, | ||
id: options.a | ||
}; | ||
} | ||
}); | ||
expect(useHook2()).toEqual({ id: "ab" }); | ||
}); | ||
|
||
test("useComposeOptions", () => { | ||
const useHook = createHook<Options, React.HTMLAttributes<any>>({ | ||
useProps(options, htmlProps) { | ||
return { | ||
...htmlProps, | ||
id: `${options.a}b` | ||
}; | ||
} | ||
}); | ||
const useHook2 = createHook<Options, React.HTMLAttributes<any>>({ | ||
compose: [useHook], | ||
useComposeOptions(options) { | ||
return { ...options, a: "a" }; | ||
} | ||
}); | ||
expect(useHook2()).toEqual({ id: "ab" }); | ||
}); | ||
|
||
test("name", () => { | ||
const useHook = createHook<Options, React.HTMLAttributes<any>>({ name: "A" }); | ||
expect(useHook.name).toBe("useA"); | ||
}); | ||
|
||
test("name and context", () => { | ||
const useHook = createHook<Options, React.HTMLAttributes<any>>({ name: "A" }); | ||
const Test = () => { | ||
return <div {...useHook()} />; | ||
}; | ||
const system = { | ||
useAProps: (_: Options, htmlProps: React.HTMLAttributes<any>) => ({ | ||
...htmlProps, | ||
id: "a" | ||
}) | ||
}; | ||
const { baseElement } = render( | ||
<SystemProvider unstable_system={system}> | ||
<Test /> | ||
</SystemProvider> | ||
); | ||
expect(baseElement).toMatchInlineSnapshot(` | ||
<body> | ||
<div> | ||
<div | ||
id="a" | ||
/> | ||
</div> | ||
</body> | ||
`); | ||
}); | ||
|
||
test("name and context with useProps", () => { | ||
const useHook = createHook<Options, React.HTMLAttributes<any>>({ | ||
name: "A", | ||
useProps(_, htmlProps) { | ||
return { className: "a", ...htmlProps }; | ||
} | ||
}); | ||
const Test = () => { | ||
return <div {...useHook()} />; | ||
}; | ||
const system = { | ||
useAProps: (_: Options, htmlProps: React.HTMLAttributes<any>) => ({ | ||
...htmlProps, | ||
id: "a" | ||
}) | ||
}; | ||
const { baseElement } = render( | ||
<SystemProvider unstable_system={system}> | ||
<Test /> | ||
</SystemProvider> | ||
); | ||
expect(baseElement).toMatchInlineSnapshot(` | ||
<body> | ||
<div> | ||
<div | ||
class="a" | ||
id="a" | ||
/> | ||
</div> | ||
</body> | ||
`); | ||
}); | ||
|
||
test("name and context with useOptions", () => { | ||
const useHook = createHook<Options, React.HTMLAttributes<any>>({ | ||
name: "A", | ||
useOptions(options) { | ||
return { | ||
...options, | ||
a: "a" | ||
}; | ||
} | ||
}); | ||
const Test = () => { | ||
return <div {...useHook()} />; | ||
}; | ||
const system = { | ||
useAProps: (options: Options, htmlProps: React.HTMLAttributes<any>) => ({ | ||
...htmlProps, | ||
id: options.a | ||
}) | ||
}; | ||
const { baseElement } = render( | ||
<SystemProvider unstable_system={system}> | ||
<Test /> | ||
</SystemProvider> | ||
); | ||
expect(baseElement).toMatchInlineSnapshot(` | ||
<body> | ||
<div> | ||
<div | ||
id="a" | ||
/> | ||
</div> | ||
</body> | ||
`); | ||
}); |
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
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.