Skip to content

Commit

Permalink
before week 10 (JavaScript)
Browse files Browse the repository at this point in the history
  • Loading branch information
Dierk Koenig committed Nov 21, 2023
1 parent 01d7db6 commit cca228b
Show file tree
Hide file tree
Showing 3 changed files with 111 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/integration-test/groovy/rooms/CalculatorSpec.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import spock.lang.Ignore
/**
* See http://www.gebish.org/manual/current/ for more instructions
*/
@Ignore
@Integration
class CalculatorSpec extends GebSpec {

Expand Down
41 changes: 41 additions & 0 deletions src/integration-test/groovy/rooms/JSTempConverterSpec.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package rooms

import geb.spock.GebSpec
import grails.testing.mixin.integration.Integration

/**
* See http://www.gebish.org/manual/current/ for more instructions
*/
@Integration
class JSTempConverterSpec extends GebSpec {

void "From celsius to fahrenheit with JavaScript"() {
when:
go '/static/Temperatures.html'
then:
title == "Temperature Converter with JavaScript"

when: "set celsius without button clicking"
$("form").celsius = "10"
$("form").fahrenheit().click()

then: "the other field is updated immediately"
$("form").fahrenheit == "50"
}

void "From fahrenheit to celsius with JavaScript"() {
when:
go '/static/Temperatures.html'
then:
title == "Temperature Converter with JavaScript"

when: "set fahrenheit without button clicking"
$("form").fahrenheit = "16"
$("form").celsius().click()

then: "the other field is updated immediately"
$("form").celsius == "-9"
}


}
69 changes: 69 additions & 0 deletions src/main/resources/public/Temperatures.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8"/>
<title>
Temperature Converter with JavaScript
</title>
<style>
main {
margin: 2rem;
font-family: "Helvetica Neue Light", sans-serif;
letter-spacing: .05em;
}
h2 {
color: hsl(240 60% 60% );
}
.converter-grid {
display: grid;
grid-template-columns: max-content 6ch;
gap: 0.5em 2em;
margin-left: 2rem;
}
input[type=number] {
text-align: right;
border: 1px solid lightgray;
}
</style>
<script>
function celsiusToFahrenheit(celsius) {
return Math.floor(celsius * 1.8 + 32);
}
function fahrenheitToCelsius(fahrenheit) {
return Math.floor((fahrenheit - 32) / 1.8);
}
</script>
</head>
<body>
<main>
<h2>
Temperature Converter with JavaScript
</h2>

<form class="converter-grid">

<label for='celsius'>Celsius</label>
<input
autofocus
id = "celsius"
name = "celsius"
type = "number"
step = "1"
value = "0"
onchange= "document.getElementById('fahrenheit').value = celsiusToFahrenheit(value);"
>

<label for='fahrenheit'>Fahrenheit</label>
<input
id = "fahrenheit"
name = "fahrenheit"
type = "number"
step = "1"
value = "32"
onchange= "document.getElementById('celsius').value = fahrenheitToCelsius(value);"
>

</form>
</main>
</body>
</html>

0 comments on commit cca228b

Please sign in to comment.