-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(unit-tests): replaced jest with vitest
## Because - We have vite as a bundler, but are using Jest as a test runner for unit tests (which also includes having a whole set of babel-related dependencies to take care of) ## This commit - Does a in-place replacement of the test runner for vitest, which also features the same API as jest, but runs much faster (82% faster) - Updates the "merge-coverage" script file
- Loading branch information
1 parent
3e942e1
commit 250848b
Showing
20 changed files
with
1,355 additions
and
1,079 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 |
---|---|---|
@@ -0,0 +1,5 @@ | ||
// jest-dom adds custom jest matchers for asserting on DOM nodes. | ||
// allows you to do things like: | ||
// expect(element).toHaveTextContent(/react/i) | ||
// learn more: https://github.com/testing-library/jest-dom | ||
import "@testing-library/jest-dom"; |
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,60 @@ | ||
/* | ||
* Please refer to the terms of the license agreement. | ||
* | ||
* (c) 2021 Feedzai, Rights Reserved. | ||
*/ | ||
import React from "react"; | ||
import { useAutoId } from "../../../src/hooks/useAutoId"; | ||
|
||
function DemoComponent({ value = null, prefix }: { value?: string | null; prefix?: string }) { | ||
const firstId = useAutoId(value, prefix); | ||
const secondId = useAutoId(); | ||
return ( | ||
<div> | ||
<p id={firstId}>A paragraph</p> | ||
<span id={secondId}>An inline span element</span> | ||
</div> | ||
); | ||
} | ||
|
||
function FallbackDemo({ | ||
value = "jd-fallback-id", | ||
prefix, | ||
}: { | ||
value?: string | null; | ||
prefix?: string; | ||
}) { | ||
const id = useAutoId(value, prefix); | ||
return <h1 id={id}>joaodias.me</h1>; | ||
} | ||
|
||
describe("useAutoId", () => { | ||
it("should generate a unique ID value", () => { | ||
cy.mount(<DemoComponent />); | ||
|
||
cy.findByText("A paragraph") | ||
.invoke("attr", "id") | ||
.then((idOne) => { | ||
cy.findByText("An inline span element").invoke("attr", "id").should("not.equal", idOne); | ||
}); | ||
}); | ||
|
||
it("should generate a prefixed unique ID value", () => { | ||
const expected = "js-a-prefix"; | ||
cy.mount(<DemoComponent value={undefined} prefix={expected} />); | ||
|
||
cy.findByText("A paragraph").invoke("attr", "id").should("contain", expected); | ||
}); | ||
|
||
it("uses a fallback ID", () => { | ||
cy.mount(<FallbackDemo />); | ||
|
||
cy.findByText("joaodias.me").should("have.id", "jd-fallback-id"); | ||
}); | ||
|
||
it("should return a prefixed fallback ID", () => { | ||
cy.mount(<FallbackDemo prefix="js-prefix" value="423696e5" />); | ||
|
||
cy.findByText("joaodias.me").should("have.id", "js-prefix--423696e5"); | ||
}); | ||
}); |
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,42 @@ | ||
import React, { useCallback, useRef, useState } from "react"; | ||
import { useFocus } from "../../../src"; | ||
|
||
function Demo({ isFocused = false, onFocus }: { isFocused?: boolean; onFocus: () => void }) { | ||
const [focused, setFocused] = useState(isFocused); | ||
|
||
const buttonRef = useRef(null); | ||
|
||
useFocus(buttonRef, focused); | ||
|
||
const handleOnClick = useCallback(() => { | ||
setFocused(true); | ||
}, [setFocused]); | ||
|
||
return ( | ||
<button ref={buttonRef} onFocus={onFocus} onClick={handleOnClick}> | ||
Demo Button | ||
</button> | ||
); | ||
} | ||
|
||
it("does not focus on mount when false", () => { | ||
cy.mount(<Demo isFocused={false} onFocus={cy.stub().as("onFocus")} />); | ||
|
||
cy.get("@onFocus").should("not.have.been.called"); | ||
}); | ||
|
||
it("focuses on mount when true", () => { | ||
cy.mount(<Demo isFocused={true} onFocus={cy.stub().as("onFocus")} />); | ||
|
||
cy.get("@onFocus").should("have.been.called"); | ||
}); | ||
|
||
it("focuses when focus value changes to true", () => { | ||
cy.mount(<Demo isFocused={false} onFocus={cy.stub().as("onFocus")} />); | ||
|
||
cy.get("@onFocus").should("not.have.been.called"); | ||
|
||
cy.get("button").click(); | ||
|
||
cy.get("@onFocus").should("have.been.called"); | ||
}); |
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.