-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #58 from skwirowski/34-answers-on-question
- Loading branch information
Showing
6 changed files
with
147 additions
and
0 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,40 @@ | ||
export const answersOnQuestion = (answers, correctAnswer, checkingAnswer) => { | ||
const app = document.querySelector('#swquiz-app'); | ||
const container = document.createElement('div'); | ||
container.setAttribute('id', 'answers-container'); | ||
container.setAttribute('data-testid', 'answers-container'); | ||
container.setAttribute('class', 'container-answers'); | ||
let chosenAnswer; | ||
let ids = ['firstAnswer', 'secondAnswer', 'thirdAnswer', 'fourthAnswer']; | ||
let buttons = ids.map((idString, index) => { | ||
let button = document.createElement('input'); | ||
button.setAttribute('type', 'button'); | ||
button.setAttribute('id', idString); | ||
button.setAttribute('data-testid', idString); | ||
button.setAttribute('class', 'button button--lighter'); | ||
button.setAttribute('value', `${answers[index]}`); | ||
return button; | ||
}); | ||
buttons.forEach((element) => { | ||
container.appendChild(element); | ||
}); | ||
app.appendChild(container); | ||
|
||
const chosenButton = document.getElementById('answers-container').querySelectorAll('.button--lighter'); | ||
|
||
for(let i = 0; i < chosenButton.length; i++) { | ||
chosenButton[i].onclick = checkingAnswer = () => { | ||
console.log(chosenButton[i].value) | ||
chosenAnswer = chosenButton[i].value; | ||
console.log(correctAnswer === chosenAnswer); | ||
if(correctAnswer === chosenAnswer) { | ||
chosenButton[i].classList.add('button--correct'); | ||
window.setTimeout('window.location.reload()', 500); | ||
} else { | ||
chosenButton[i].classList.add('button--wrong'); | ||
window.setTimeout('window.location.reload()', 500); | ||
} | ||
} | ||
} | ||
|
||
} |
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,11 @@ | ||
.container-answers { | ||
display: grid; | ||
width: 1200px; | ||
min-height: 350px; | ||
grid-template-columns: 1fr 1fr; | ||
grid-template-rows: 100px 100px; | ||
column-gap: 50px; | ||
row-gap: 50px; | ||
justify-content: center; | ||
margin: 50px; | ||
} |
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,38 @@ | ||
.button { | ||
border-radius: 16px; | ||
font-size: 42px; | ||
font-weight: 500; | ||
height: 100px; | ||
line-height: 51.2px; | ||
width: 600px; | ||
} | ||
|
||
.button--red { | ||
background: rgba(255, 0, 0, 0.8); | ||
box-shadow: 0px 4px 4px rgba(0, 0, 0, 0.25); | ||
color: #fff; | ||
} | ||
|
||
.button--uppercase { | ||
text-transform: uppercase; | ||
} | ||
|
||
.button--lighter { | ||
font-weight: 400; | ||
} | ||
|
||
.button--lighter:hover { | ||
box-shadow: 0px 4px 4px rgba(0, 0, 0, 0.25), inset 0px 4px 4px rgba(0, 0, 0, 0.25); | ||
} | ||
|
||
.button--wrong { | ||
font-weight: 600; | ||
background: #FF0000; | ||
box-shadow: 0px 4px 4px rgba(0, 0, 0, 0.25), 0px 4px 30px #FF0000; | ||
} | ||
|
||
.button--correct { | ||
font-weight: 600; | ||
background: #41ED25; | ||
box-shadow: 0px 4px 4px rgba(0, 0, 0, 0.25), 0px 4px 30px #51FC00; | ||
} |
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,34 @@ | ||
import { answersOnQuestion } from '../src/app/components/answersOnQuestion'; | ||
import { fireEvent, screen } from '@testing-library/dom'; | ||
import '@testing-library/jest-dom/extend-expect'; | ||
|
||
|
||
test('div has class - container-answers', () => { | ||
document.body.innerHTML = `<div id="swquiz-app"></div>`; | ||
answersOnQuestion(['Luke', 'Angel', 'Becka', 'John'], 'Luke'); | ||
expect(screen.getByTestId('answers-container')).toHaveClass('container-answers'); | ||
}); | ||
|
||
test('div has buttons inside', () => { | ||
document.body.innerHTML = `<div id="swquiz-app"></div>`; | ||
answersOnQuestion(['Luke', 'Angel', 'Becka', 'John'], 'Luke'); | ||
expect(screen.getByTestId('answers-container')).not.toBeEmptyDOMElement(); | ||
}); | ||
|
||
test('Correct answer was cliked', () => { | ||
document.body.innerHTML = `<div id="swquiz-app"></div>`; | ||
answersOnQuestion(['Luke', 'Angel', 'Becka', 'John'], 'Luke'); | ||
const button = document.getElementById('firstAnswer'); | ||
fireEvent.click(button); | ||
expect(screen.getByTestId('firstAnswer')).toHaveClass('button button--lighter button--correct'); | ||
}); | ||
|
||
test('Wrong answer was cliked', () => { | ||
document.body.innerHTML = `<div id="swquiz-app"></div>`; | ||
answersOnQuestion(['Luke', 'Angel', 'Becka', 'John'], 'John'); | ||
const button = document.getElementById('firstAnswer'); | ||
fireEvent.click(button); | ||
expect(screen.getByTestId('firstAnswer')).toHaveClass('button button--lighter button--wrong'); | ||
}); | ||
|
||
|