-
-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
change quote references => affirmation and add unit tests
- Loading branch information
Showing
8 changed files
with
188 additions
and
94 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 was deleted.
Oops, something went wrong.
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,54 @@ | ||
import React from "react"; | ||
|
||
import "./App.css"; | ||
|
||
import affirmations from "./affirmations"; | ||
|
||
export function getRandomInt(max) { | ||
return Math.floor(Math.random() * Math.floor(max)); | ||
} | ||
|
||
export function getAffirmation(index) { | ||
return { | ||
author: affirmations[index].author, | ||
affirmation: affirmations[index].affirmation | ||
}; | ||
} | ||
|
||
export function generateAffirmation() { | ||
const index = getRandomInt(affirmations.length); | ||
const affirmation = getAffirmation(index); | ||
|
||
return affirmation; | ||
} | ||
|
||
class AffirmationComponent extends React.Component { | ||
constructor(props) { | ||
super(props); | ||
|
||
this.state = { | ||
affirmation: generateAffirmation() | ||
}; | ||
|
||
this.handleClick = this.handleClick.bind(this); | ||
} | ||
|
||
handleClick(e) { | ||
this.setState({ affirmation: generateAffirmation() }); | ||
} | ||
|
||
render() { | ||
const affirmation = this.state.affirmation; | ||
return ( | ||
<div className="Affirmation"> | ||
<blockquote className="affirmation"> | ||
{affirmation.affirmation} | ||
</blockquote> | ||
<p className="author-citation">{affirmation.author}</p> | ||
<button onClick={this.handleClick}>Next Affirmation</button> | ||
</div> | ||
); | ||
} | ||
} | ||
|
||
export default AffirmationComponent; |
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,83 @@ | ||
import React from "react"; | ||
import ReactDOM from "react-dom"; | ||
import AffirmationComponent, { | ||
getRandomInt, | ||
generateAffirmation, | ||
getAffirmation | ||
} from "./AffirmationComponent"; | ||
|
||
const MOCK_AFFIRMATIONS = [ | ||
{ | ||
affirmation: | ||
"If you live to be a hundred, I want to live to be a hundred minus one day so I never have to live without you.", | ||
author: "Winnie The Pooh" | ||
}, | ||
{ | ||
affirmation: "OK. I’m thinking of something orange, and it’s small...", | ||
author: "Dory" | ||
} | ||
]; | ||
|
||
jest.mock("./affirmations", () => { | ||
const affirmations = [ | ||
{ | ||
affirmation: | ||
"If you live to be a hundred, I want to live to be a hundred minus one day so I never have to live without you.", | ||
author: "Winnie The Pooh" | ||
}, | ||
{ | ||
affirmation: "OK. I’m thinking of something orange, and it’s small...", | ||
author: "Dory" | ||
} | ||
]; | ||
|
||
return affirmations; | ||
}); | ||
|
||
it("renders without crashing", () => { | ||
const div = document.createElement("div"); | ||
ReactDOM.render(<AffirmationComponent />, div); | ||
ReactDOM.unmountComponentAtNode(div); | ||
}); | ||
|
||
describe("getRandomInt()", () => { | ||
it("returns a random number less than max of 10", () => { | ||
const integer = getRandomInt(10); | ||
expect(integer).toBeLessThanOrEqual(9); | ||
}); | ||
|
||
it("returns a random number less than max of 3", () => { | ||
const integer = getRandomInt(3); | ||
expect(integer).toBeLessThanOrEqual(3); | ||
}); | ||
}); | ||
|
||
describe("generateAffirmation()", () => { | ||
it("should return a affirmation with an author and affirmation", () => { | ||
const affirmation = generateAffirmation(); | ||
|
||
expect(affirmation).toStrictEqual( | ||
expect.objectContaining({ | ||
author: expect.any(String), | ||
affirmation: expect.any(String) | ||
}) | ||
); | ||
}); | ||
}); | ||
|
||
describe("getAffirmation()", () => { | ||
it("should return a specific affirmation based on index", () => { | ||
const affirmation = getAffirmation(1); | ||
|
||
expect(affirmation).toStrictEqual({ | ||
author: MOCK_AFFIRMATIONS[1].author, | ||
affirmation: MOCK_AFFIRMATIONS[1].affirmation | ||
}); | ||
|
||
const affirmationAlt = getAffirmation(0); | ||
expect(affirmationAlt).toStrictEqual({ | ||
author: MOCK_AFFIRMATIONS[0].author, | ||
affirmation: MOCK_AFFIRMATIONS[0].affirmation | ||
}); | ||
}); | ||
}); |
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.