-
Notifications
You must be signed in to change notification settings - Fork 4
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
24 changed files
with
749 additions
and
10 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
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,5 +1,6 @@ | ||
# react-library | ||
|
||
- [`@dolthub/react-contexts`](https://github.com/dolthub/react-library/tree/main/packages/contexts): A library of useful React contexts | ||
- [`@dolthub/react-hooks`](https://github.com/dolthub/react-library/tree/main/packages/hooks): A library of useful React hooks | ||
- [`@dolthub/web-utils`](https://github.com/dolthub/react-library/tree/main/packages/utils): A library of useful utilities for the web | ||
- [`@dolthub/proto-resource-utils`](https://github.com/dolthub/react-library/tree/main/packages/resource-utils): A library of useful utilities for managing Google protobuf resources and names | ||
- [`@dolthub/web-utils`](https://github.com/dolthub/react-library/tree/main/packages/utils): A library of useful utilities for the web |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
dist | ||
node_modules | ||
.eslintrc.js | ||
./*.js |
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,3 @@ | ||
module.exports = { | ||
extends: "../../.eslintrc.cjs", | ||
}; |
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,2 @@ | ||
src/ | ||
tsconfig.* |
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,15 @@ | ||
# @dolthub/react-contexts | ||
|
||
A library of useful React contexts. | ||
|
||
## Installation | ||
|
||
``` | ||
% yarn add @dolthub/react-contexts | ||
``` | ||
|
||
or | ||
|
||
``` | ||
% npm install @dolthub/react-contexts | ||
``` |
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,7 @@ | ||
module.exports = { | ||
presets: [ | ||
["@babel/preset-env", { targets: { node: "current" } }], | ||
"@babel/preset-react", | ||
"@babel/preset-typescript", | ||
], | ||
}; |
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,13 @@ | ||
const TEST_REGEX = "(/__tests__/.*|(\\.|/)(test|spec))\\.(jsx?|js?|tsx?|ts?)$"; | ||
|
||
module.exports = { | ||
setupFilesAfterEnv: ["<rootDir>/jest.setup.ts"], | ||
testRegex: TEST_REGEX, | ||
transform: { | ||
"^.+\\.tsx?$": "babel-jest", | ||
}, | ||
testPathIgnorePatterns: ["types", "node_modules", ".rollup.cache", "dist"], | ||
moduleFileExtensions: ["ts", "js", "tsx"], | ||
collectCoverage: false, | ||
clearMocks: true, | ||
}; |
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,2 @@ | ||
import "@testing-library/jest-dom"; | ||
import "@testing-library/react"; |
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,82 @@ | ||
{ | ||
"name": "@dolthub/react-contexts", | ||
"author": "DoltHub", | ||
"description": "A collection of React contexts for common tasks", | ||
"version": "0.1.0", | ||
"main": "dist/cjs/index.js", | ||
"module": "dist/esm/index.js", | ||
"types": "dist/index.d.ts", | ||
"files": [ | ||
"dist" | ||
], | ||
"keywords": [ | ||
"react", | ||
"contexts", | ||
"react-contexts", | ||
"state", | ||
"frontend" | ||
], | ||
"packageManager": "[email protected]", | ||
"scripts": { | ||
"compile": "tsc -b", | ||
"build": "rollup -c --bundleConfigAsCjs", | ||
"dbuild": "yarn compile && yarn build", | ||
"lint": "eslint --cache --ext .ts,.js,.tsx,.jsx src", | ||
"prettier": "prettier --check 'src/**/*.{js,ts,tsx}'", | ||
"prettier-fix": "prettier --write 'src/**/*.{js,ts,tsx}'", | ||
"npm:publish": "yarn dbuild && npm publish", | ||
"test": "jest --env=jest-environment-jsdom", | ||
"yalc:publish": "yarn dbuild && yalc publish", | ||
"yalc:push": "yarn dbuild && yalc push" | ||
}, | ||
"peerDependencies": { | ||
"react": "^18.2.0", | ||
"react-dom": "^18.2.0" | ||
}, | ||
"dependencies": { | ||
"@dolthub/react-hooks": "^0.1.6", | ||
"swr": "^2.2.4" | ||
}, | ||
"devDependencies": { | ||
"@babel/core": "^7.23.7", | ||
"@babel/preset-env": "^7.23.8", | ||
"@babel/preset-react": "^7.23.3", | ||
"@babel/preset-typescript": "^7.23.3", | ||
"@rollup/plugin-commonjs": "^25.0.7", | ||
"@rollup/plugin-node-resolve": "^15.2.3", | ||
"@rollup/plugin-typescript": "^11.1.5", | ||
"@testing-library/jest-dom": "^6.2.0", | ||
"@testing-library/react": "^14.1.2", | ||
"@testing-library/react-hooks": "^8.0.1", | ||
"@types/babel__core": "^7", | ||
"@types/babel__preset-env": "^7", | ||
"@types/eslint": "^8", | ||
"@types/jest": "^29.5.11", | ||
"@types/react": "^18", | ||
"@types/react-dom": "^18", | ||
"@types/rollup-plugin-peer-deps-external": "^2", | ||
"@typescript-eslint/eslint-plugin": "^6.18.1", | ||
"@typescript-eslint/parser": "^6.18.1", | ||
"babel-jest": "^29.7.0", | ||
"eslint": "^8.56.0", | ||
"jest": "^29.7.0", | ||
"jest-environment-jsdom": "^29.7.0", | ||
"prettier": "^3.1.0", | ||
"react": "^18.2.0", | ||
"react-dom": "^18.2.0", | ||
"rollup": "^4.9.4", | ||
"rollup-plugin-dts": "^6.1.0", | ||
"rollup-plugin-peer-deps-external": "^2.2.4", | ||
"rollup-plugin-terser": "^7.0.2", | ||
"tslib": "^2.6.2", | ||
"typescript": "^5.3.3", | ||
"yalc": "^1.0.0-pre.53" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "git+https://github.com/dolthub/react-library.git" | ||
}, | ||
"bugs": { | ||
"url": "https://github.com/dolthub/react-library/issues" | ||
} | ||
} |
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,39 @@ | ||
import resolve from "@rollup/plugin-node-resolve"; | ||
import commonjs from "@rollup/plugin-commonjs"; | ||
import typescript from "@rollup/plugin-typescript"; | ||
import { terser } from "rollup-plugin-terser"; | ||
import external from "rollup-plugin-peer-deps-external"; | ||
import { dts } from "rollup-plugin-dts"; | ||
|
||
const packageJson = require("./package.json"); | ||
|
||
export default [ | ||
{ | ||
input: "src/index.ts", | ||
output: [ | ||
{ | ||
file: packageJson.main, | ||
format: "cjs", | ||
sourcemap: true, | ||
name: "react-ts-lib", | ||
}, | ||
{ | ||
file: packageJson.module, | ||
format: "esm", | ||
sourcemap: true, | ||
}, | ||
], | ||
plugins: [ | ||
external(), | ||
resolve(), | ||
commonjs(), | ||
typescript({ tsconfig: "./tsconfig.json" }), | ||
terser(), | ||
], | ||
}, | ||
{ | ||
input: "./types/index.d.ts", | ||
output: [{ file: "dist/index.d.ts", format: "esm" }], | ||
plugins: [dts()], | ||
}, | ||
]; |
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,109 @@ | ||
import { act, fireEvent, render, screen } from "@testing-library/react"; | ||
import React from "react"; | ||
import CommentFormProvider, { useCommentFormContext } from "../commentForm"; | ||
|
||
const MockComponent: React.FC = () => { | ||
const { | ||
comment, | ||
setComment, | ||
onSubmit, | ||
commentFormIsVisible, | ||
commentFormRef, | ||
} = useCommentFormContext(); | ||
return ( | ||
<div ref={commentFormRef}> | ||
<textarea | ||
value={comment} | ||
onChange={e => setComment(e.target.value)} | ||
data-testid="textarea" | ||
/> | ||
<button onClick={onSubmit}>Submit</button> | ||
{commentFormIsVisible && <div>Form is visible</div>} | ||
</div> | ||
); | ||
}; | ||
|
||
describe("CommentFormProvider", () => { | ||
beforeEach(() => { | ||
window.HTMLElement.prototype.scrollIntoView = function () {}; | ||
}); | ||
|
||
it("initializes with default values", () => { | ||
render( | ||
<CommentFormProvider | ||
containerId="testContainer" | ||
commentId="testComment" | ||
createComment={jest.fn()} | ||
loading={false} | ||
> | ||
<MockComponent /> | ||
</CommentFormProvider>, | ||
); | ||
|
||
expect(screen.getByText("Submit")).toBeInTheDocument(); | ||
expect(screen.getByTestId("textarea")).toHaveValue(""); | ||
expect(screen.getByText("Form is visible")).toBeInTheDocument(); | ||
}); | ||
|
||
it("updates comment on input change", () => { | ||
render( | ||
<CommentFormProvider | ||
containerId="testContainer" | ||
commentId="testComment" | ||
createComment={jest.fn()} | ||
loading={false} | ||
> | ||
<MockComponent /> | ||
</CommentFormProvider>, | ||
); | ||
|
||
const input: HTMLTextAreaElement = screen.getByTestId("textarea"); | ||
fireEvent.change(input, { target: { value: "New Comment" } }); | ||
|
||
expect(input.value).toBe("New Comment"); | ||
}); | ||
|
||
it("submits the form correctly", async () => { | ||
const mockCreateComment = jest.fn().mockResolvedValue(true); | ||
render( | ||
<CommentFormProvider | ||
containerId="testContainer" | ||
commentId="testComment" | ||
createComment={mockCreateComment} | ||
loading={false} | ||
> | ||
<MockComponent /> | ||
</CommentFormProvider>, | ||
); | ||
|
||
const input: HTMLTextAreaElement = screen.getByTestId("textarea"); | ||
fireEvent.change(input, { target: { value: "New Comment" } }); | ||
fireEvent.click(screen.getByText("Submit")); | ||
|
||
await act(async () => { | ||
expect(mockCreateComment).toHaveBeenCalledWith("New Comment"); | ||
}); | ||
expect(input.value).toBe(""); | ||
}); | ||
|
||
it("handles form visibility correctly during window resize", () => { | ||
render( | ||
<CommentFormProvider | ||
containerId="testContainer" | ||
commentId="testComment" | ||
createComment={jest.fn()} | ||
loading={false} | ||
> | ||
<MockComponent /> | ||
</CommentFormProvider>, | ||
); | ||
|
||
act(() => { | ||
// Simulate window resize | ||
global.innerWidth = 500; | ||
global.dispatchEvent(new Event("resize")); | ||
}); | ||
|
||
expect(screen.getByText("Form is visible")).toBeInTheDocument(); | ||
}); | ||
}); |
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,29 @@ | ||
import { | ||
FeatureMap, | ||
getFeatures, | ||
getFeaturesForShowAll, | ||
} from "../features/feature"; | ||
|
||
const showAllMap: FeatureMap = new Map([ | ||
["bounties", true], | ||
["test", true], | ||
["marketing", true], | ||
]); | ||
|
||
const isDev = false; | ||
const initialMap: FeatureMap = new Map([ | ||
["bounties", true], | ||
["test", false], | ||
["marketing", isDev], | ||
]); | ||
|
||
describe("test getFeatures", () => { | ||
it("returns feature map", () => { | ||
expect(getFeatures(initialMap)).toStrictEqual(initialMap); | ||
expect(getFeatures(initialMap, false)).toStrictEqual(initialMap); | ||
expect(getFeatures(initialMap, true)).toStrictEqual(showAllMap); | ||
expect(getFeatures(initialMap, false)).toStrictEqual(initialMap); | ||
expect(getFeatures(initialMap, true)).toStrictEqual(showAllMap); | ||
expect(getFeaturesForShowAll(initialMap)).toStrictEqual(showAllMap); | ||
}); | ||
}); |
Oops, something went wrong.