Skip to content

Commit 16932f9

Browse files
authored
Merge pull request #13 from Raffaello/page_links
added flash test and page links in the / refactor flash template
2 parents 884b643 + 50c217f commit 16932f9

11 files changed

+60
-56
lines changed

server/app/controllers/HangPerson.scala

+4-4
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,15 @@ class HangPerson @Inject()(val messagesApi: MessagesApi, val ws: WSClient, cache
2323
val uuid = request.session.get(sessionKey)
2424
if (uuid.isEmpty) {
2525
Future.successful(
26-
Redirect(routes.HangPerson.newAction()).flashing("error" -> "Game was not created!")
26+
Redirect(routes.HangPerson.newAction()).flashing("danger" -> "Game was not created!")
2727
)
2828
} else {
2929
val maybeGame: Option[HangPersonGame] = cache.get[HangPersonGame](uuid.get)
3030
maybeGame match {
3131
case None => {
3232
Future.successful(
3333
Redirect(routes.HangPerson.newAction())
34-
.flashing("error" -> "Game was not found!"))
34+
.flashing("danger" -> "Game was not found!"))
3535
}
3636
case _ => {
3737
game = maybeGame.get
@@ -97,11 +97,11 @@ class HangPerson @Inject()(val messagesApi: MessagesApi, val ws: WSClient, cache
9797
if (game.guess(HangPersonData.letter)) {
9898
Redirect(routes.HangPerson.show())
9999
} else {
100-
Redirect(routes.HangPerson.show()).flashing("error" -> "You have already used that letter.")
100+
Redirect(routes.HangPerson.show()).flashing("danger" -> "You have already used that letter.")
101101
}
102102
} catch {
103103
case e: IllegalArgumentException =>
104-
Redirect(routes.HangPerson.show()).flashing("error" -> e.getMessage)
104+
Redirect(routes.HangPerson.show()).flashing("danger" -> e.getMessage)
105105
}
106106
}
107107
)

server/app/views/HangPerson/base.scala.html

+4-2
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,10 @@ <h1>Play Hangperson!</h1>
1818
</div>
1919
<div class="panel panel-default">
2020
<div>
21-
@if(flash.get("error").isDefined) {
22-
@alerts_macro("error", flash("error"))
21+
@if(!flash.isEmpty) {
22+
@for((k,v) <- flash.data) {
23+
@alerts_macro(k, v)
24+
}
2325
}
2426
</div>
2527
@content
+2-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
@(game_word: String)(implicit flash: Flash)
2-
@base {
2+
@views.html.HangPerson.base {
33
<h1>Sorry, you lose!</h1>
44

55
<p>
66
The word was
77
<span class="text-lose">@game_word</span>
88
</p>
99

10-
@newActionSnippet()
10+
@views.html.HangPerson.newActionSnippet()
1111
}
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
@()(implicit flash: Flash)
2-
@base {
3-
@newActionSnippet()
2+
@views.html.HangPerson.base {
3+
@views.html.HangPerson.newActionSnippet()
44
}

server/app/views/HangPerson/show.scala.html

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
@(game_wrong_guesses: String, game_word_with_guesses: String, hangPersonForm: Form[HangPersonData])(implicit messages: Messages, flash: Flash)
66

7-
@base {
7+
@views.html.HangPerson.base {
88
<div class="panel-body">
99
<h2>Guess a letter</h2>
1010
<p>
@@ -38,5 +38,5 @@ <h2>Guess a letter</h2>
3838
}
3939
</div>
4040

41-
@newActionSnippet()
41+
@views.html.HangPerson.newActionSnippet()
4242
}
+2-3
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
@(game_word: String)(implicit flash: Flash)
2-
@base {
2+
@views.html.HangPerson.base {
33
<h1>You Win!</h1>
4-
54
<p>
65
The word was
76
<span class="text-win">@game_word</span>.
87
</p>
98

10-
@newActionSnippet()
9+
@views.html.HangPerson.newActionSnippet()
1110
}
+10-30
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,12 @@
11
@(level: String, message: String)
22

3-
@level match {
4-
case "success" => {
5-
<div class="alert alert-success">
6-
<strong>Success!</strong>
7-
@message
8-
</div>
9-
}
10-
11-
case "info" => {
12-
<div class="alert alert-info">
13-
<strong>Info!</strong>
14-
@message
15-
</div>
16-
}
17-
case "warning" => {
18-
<div class="alert alert-warning">
19-
<strong>Warning!</strong>
20-
@message
21-
</div>
22-
}
23-
case "danger" => {
24-
<div class="alert alert-danger">
25-
<strong>Error!</strong>
26-
@message
27-
</div>
28-
}
29-
case "error" => {
30-
@this("danger", message)
31-
}
32-
}
3+
<div class="alert alert-@{level}" data-dismiss="alert">
4+
<strong>
5+
@if("danger" == level) {
6+
Error!
7+
} else {
8+
@{level.toString.capitalize}!
9+
}
10+
</strong>
11+
@message
12+
</div>

server/app/views/index.scala.html

+4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
@(message: String)
22

33
@main("Welcome to Play") {
4+
<div class="btn-group">
5+
<a id="hangman" class="btn btn-primary" href="@routes.HangPerson.newAction()">Go to HangMan Game</a>
6+
<a id="hangman-spa" class="btn btn-primary" href="@routes.HangPersonSpa.index()">Go to HangMan Game SPA</a>
7+
</div>
48
@play20.welcome(message)
59
}

server/test/ApplicationSpec.scala

+15-3
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,14 @@
1-
import org.specs2.mutable._
21
import org.specs2.runner._
32
import org.junit.runner._
43
import play.api.test._
5-
import play.api.test.Helpers._
64

75
/**
86
* Add your spec here.
97
* You can mock out a whole application including requests, plugins etc.
108
* For more information, consult the wiki.
119
*/
1210
@RunWith(classOf[JUnitRunner])
13-
class ApplicationSpec extends Specification {
11+
class ApplicationSpec extends PlaySpecification {
1412
// Set sequential execution
1513
sequential
1614

@@ -25,5 +23,19 @@ class ApplicationSpec extends Specification {
2523
contentType(home) must beSome.which(_ == "text/html")
2624
contentAsString(home) must contain("Your new application is ready.")
2725
}
26+
27+
"click on HangMan redirect to /newgame" in new WithBrowser {
28+
browser.goTo("http://localhost:" + port)
29+
browser.pageSource must contain("HangMan Game")
30+
browser.click("a[id='hangman']")
31+
browser.url() must contain("/new")
32+
}
33+
34+
"click on HangMan redirect to /spa" in new WithBrowser {
35+
browser.goTo("http://localhost:" + port)
36+
browser.pageSource must contain("HangMan Game SPA")
37+
browser.click("a[id='hangman-spa']")
38+
browser.url() must contain("/hangperson/spa")
39+
}
2840
}
2941
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package controllers
2+
3+
import org.junit.runner.RunWith
4+
import org.specs2.runner.JUnitRunner
5+
import play.api.test.{PlaySpecification, WithBrowser}
6+
7+
@RunWith(classOf[JUnitRunner])
8+
class HanPersonSpaSpec extends PlaySpecification {
9+
"work from within a browser SPA" in new WithBrowser {
10+
browser.goTo("http://localhost:" + port + "/hangperson/spa")
11+
browser.pageSource must contain("Hang Person SPA")
12+
}
13+
}

server/test/controllers/HangPersonSpec.scala

+2-8
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import play.api.test.Helpers._
88
import play.api.test._
99

1010
@RunWith(classOf[JUnitRunner])
11-
class HangPersonSpec extends Specification
11+
class HangPersonSpec extends PlaySpecification
1212
{
1313
// Set sequential execution
1414
sequential
@@ -55,24 +55,18 @@ class HangPersonSpec extends Specification
5555
browser.pageSource must contain("Hangperson")
5656
}
5757

58-
"work from within a browser SPA" in new WithBrowser {
59-
browser.goTo("http://localhost:" + port + "/hangperson/spa")
60-
browser.pageSource must contain("Hang Person SPA")
61-
}
62-
6358
"work from a browser click new game and go to show page" in new WithBrowser {
6459
browser.goTo("http://localhost:" + port + "/hangperson/new")
6560
browser.pageSource must contain("New Game")
6661
browser.click("input[id='newgame']")
6762
browser.url() must contain("/show")
68-
println(browser.url())
6963
}
7064
}
7165

7266
// these are useless test, but just for learning...
7367
"HangPerson VIEWS" should {
7468
"render new" in new WithApplication {
75-
contentAsString(views.html.HangPerson.newAction()(new Flash())) must contain("New Game")
69+
contentAsString(views.html.HangPerson.newAction()(new Flash(Map("success" -> "test")))) must contain("Success!")
7670
}
7771
}
7872
}

0 commit comments

Comments
 (0)