Permalink
Browse files

component: Add Now field to Present.

Previously, Present always used time.Now() to get the current
local time. After this change, it uses its Now field for that.

This makes it possible to mock time, so that tests continue to pass
no matter what time they're run.
  • Loading branch information...
dmitshur committed Oct 7, 2018
1 parent 6097cdf commit abdf68eba34d4420c7c92e73c11bf8fce2a86712
Showing with 15 additions and 7 deletions.
  1. +7 −3 component/date.go
  2. +4 −3 resume.go
  3. +4 −1 resume_test.go
View
@@ -28,10 +28,14 @@ func (d Date) Render() []*html.Node {
}
// Present represents the present date.
type Present struct{}
type Present struct {
// Now should be set to the current local time.
// Tests may use a fixed time to get reproducible output.
Now time.Time
}
func (Present) Date() (year int, month time.Month) {
year, month, _ = time.Now().UTC().Date()
func (p Present) Date() (year int, month time.Month) {
year, month, _ = p.Now.UTC().Date()
return year, month
}
View
@@ -12,9 +12,10 @@ import (
)
// DmitriShuralyov returns Dmitri Shuralyov's resume,
// using the basic user information of shurcool.
// using the basic user information of shurcool,
// and the current local time now.
// It's annotated with the given reactions from the perspective of currentUser.
func DmitriShuralyov(shurcool users.User, reactions map[string][]reactions.Reaction, currentUser users.User) htmlg.Component {
func DmitriShuralyov(shurcool users.User, now time.Time, reactions map[string][]reactions.Reaction, currentUser users.User) htmlg.Component {
// reactable is a convenience helper that joins reactable content with its ReactionsBar,
// using id as reactable ID. It populates ReactionsBar's Reactions and CurrentUser fields.
reactable := func(id string, content htmlg.Component) htmlg.Component {
@@ -44,7 +45,7 @@ func DmitriShuralyov(shurcool users.User, reactions map[string][]reactions.React
Title: "Software Engineer",
Subtitle: "Google",
Dates: resumecomponent.DateRange{
From: resumecomponent.Date{Year: 2018, Month: time.July}, To: resumecomponent.Present{},
From: resumecomponent.Date{Year: 2018, Month: time.July}, To: resumecomponent.Present{Now: now},
},
Lines: []htmlg.Component{
component.Join("Joined the Go Open Source Project team."),
View
@@ -6,6 +6,7 @@ import (
"io/ioutil"
"path/filepath"
"testing"
"time"
"github.com/shurcooL/htmlg"
"github.com/shurcooL/reactions"
@@ -24,6 +25,8 @@ func TestDmitriShuralyov(t *testing.T) {
Email: "dmitri@shuralyov.com",
}
mockTime = time.Date(2018, time.August, 26, 9, 41, 0, 0, time.UTC)
alice = users.User{UserSpec: users.UserSpec{ID: 1, Domain: "example.org"}, Login: "Alice"}
bob = users.User{UserSpec: users.UserSpec{ID: 2, Domain: "example.org"}, Login: "Bob"}
@@ -39,7 +42,7 @@ func TestDmitriShuralyov(t *testing.T) {
)
var buf bytes.Buffer
err := htmlg.RenderComponents(&buf, resume.DmitriShuralyov(shurcool, mockReactions, alice))
err := htmlg.RenderComponents(&buf, resume.DmitriShuralyov(shurcool, mockTime, mockReactions, alice))
if err != nil {
t.Fatal(err)
}

0 comments on commit abdf68e

Please sign in to comment.