Skip to content

Commit

Permalink
refactor: use context in watchlist
Browse files Browse the repository at this point in the history
  • Loading branch information
achannarasappa committed Feb 14, 2021
1 parent 6321eeb commit 590ee57
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 18 deletions.
11 changes: 6 additions & 5 deletions internal/ui/component/watchlist/watchlist.go
Expand Up @@ -5,6 +5,7 @@ import (
"strconv"
"strings"

c "github.com/achannarasappa/ticker/internal/common"
. "github.com/achannarasappa/ticker/internal/position"
. "github.com/achannarasappa/ticker/internal/quote"
. "github.com/achannarasappa/ticker/internal/sorter"
Expand All @@ -24,13 +25,13 @@ type Model struct {
}

// NewModel returns a model with default values.
func NewModel(separate bool, extraInfoExchange bool, extraInfoFundamentals bool, sort string) Model {
func NewModel(ctx c.Context) Model {
return Model{
Width: 80,
Separate: separate,
ExtraInfoExchange: extraInfoExchange,
ExtraInfoFundamentals: extraInfoFundamentals,
Sorter: NewSorter(sort),
Separate: ctx.Config.Separate,
ExtraInfoExchange: ctx.Config.ExtraInfoExchange,
ExtraInfoFundamentals: ctx.Config.ExtraInfoFundamentals,
Sorter: NewSorter(ctx.Config.Sort),
}
}

Expand Down
109 changes: 97 additions & 12 deletions internal/ui/component/watchlist/watchlist_test.go
Expand Up @@ -9,6 +9,7 @@ import (
. "github.com/onsi/ginkgo/extensions/table"
. "github.com/onsi/gomega"

c "github.com/achannarasappa/ticker/internal/common"
. "github.com/achannarasappa/ticker/internal/position"
. "github.com/achannarasappa/ticker/internal/quote"
. "github.com/achannarasappa/ticker/internal/ui/component/watchlist"
Expand Down Expand Up @@ -37,7 +38,14 @@ var _ = Describe("Watchlist", func() {
}
}

m := NewModel(false, false, false, "")
m := NewModel(c.Context{
Config: c.Config{
Separate: false,
ExtraInfoExchange: false,
ExtraInfoFundamentals: false,
Sort: "",
},
})
m.Width = 80
m.Positions = positionMap
m.Quotes = []Quote{
Expand Down Expand Up @@ -201,7 +209,14 @@ var _ = Describe("Watchlist", func() {
When("there are more than one symbols on the watchlist", func() {
It("should render a watchlist with each symbol", func() {

m := NewModel(false, false, false, "")
m := NewModel(c.Context{
Config: c.Config{
Separate: false,
ExtraInfoExchange: false,
ExtraInfoFundamentals: false,
Sort: "",
},
})
m.Width = 80
m.Quotes = []Quote{
{
Expand Down Expand Up @@ -265,7 +280,14 @@ var _ = Describe("Watchlist", func() {
When("the show-separator layout flag is set", func() {
It("should render a watchlist with separators", func() {

m := NewModel(true, false, false, "")
m := NewModel(c.Context{
Config: c.Config{
Separate: true,
ExtraInfoExchange: false,
ExtraInfoFundamentals: false,
Sort: "",
},
})
m.Quotes = []Quote{
{
ResponseQuote: ResponseQuote{
Expand Down Expand Up @@ -318,7 +340,14 @@ var _ = Describe("Watchlist", func() {

When("the option for extra exchange information is set", func() {
It("should render extra exchange information", func() {
m := NewModel(true, true, false, "")
m := NewModel(c.Context{
Config: c.Config{
Separate: true,
ExtraInfoExchange: true,
ExtraInfoFundamentals: false,
Sort: "",
},
})
m.Quotes = []Quote{
{
ResponseQuote: ResponseQuote{
Expand All @@ -345,7 +374,14 @@ var _ = Describe("Watchlist", func() {

When("the exchange has a delay", func() {
It("should render extra exchange information with the delay amount", func() {
m := NewModel(true, true, false, "")
m := NewModel(c.Context{
Config: c.Config{
Separate: true,
ExtraInfoExchange: true,
ExtraInfoFundamentals: false,
Sort: "",
},
})
m.Quotes = []Quote{
{
ResponseQuote: ResponseQuote{
Expand Down Expand Up @@ -374,7 +410,14 @@ var _ = Describe("Watchlist", func() {

When("the option for extra fundamental information is set", func() {
It("should render extra fundamental information", func() {
m := NewModel(true, false, true, "")
m := NewModel(c.Context{
Config: c.Config{
Separate: true,
ExtraInfoExchange: false,
ExtraInfoFundamentals: true,
Sort: "",
},
})
m.Quotes = []Quote{
{
ResponseQuote: ResponseQuote{
Expand All @@ -401,7 +444,14 @@ var _ = Describe("Watchlist", func() {

When("there is no day range", func() {
It("should not render the day range field", func() {
m := NewModel(true, false, true, "")
m := NewModel(c.Context{
Config: c.Config{
Separate: true,
ExtraInfoExchange: false,
ExtraInfoFundamentals: true,
Sort: "",
},
})
m.Quotes = []Quote{
{
ResponseQuote: ResponseQuote{
Expand Down Expand Up @@ -429,7 +479,14 @@ var _ = Describe("Watchlist", func() {

When("the option for sort is set to 'alpha'", func() {
It("should render quotes alphabetically", func() {
m := NewModel(true, false, false, "alpha")
m := NewModel(c.Context{
Config: c.Config{
Separate: true,
ExtraInfoExchange: false,
ExtraInfoFundamentals: false,
Sort: "alpha",
},
})
m.Quotes = []Quote{
{
ResponseQuote: ResponseQuote{
Expand Down Expand Up @@ -484,7 +541,14 @@ var _ = Describe("Watchlist", func() {

When("the option for sort is set to 'value'", func() {
It("should render quotes by position value, with inactive quotes last", func() {
m := NewModel(true, false, false, "value")
m := NewModel(c.Context{
Config: c.Config{
Separate: true,
ExtraInfoExchange: false,
ExtraInfoFundamentals: false,
Sort: "value",
},
})
m.Quotes = []Quote{
{
ResponseQuote: ResponseQuote{
Expand Down Expand Up @@ -586,7 +650,14 @@ var _ = Describe("Watchlist", func() {

When("the sort option isn't set", func() {
It("should render quotes by change percent", func() {
m := NewModel(true, false, false, "")
m := NewModel(c.Context{
Config: c.Config{
Separate: true,
ExtraInfoExchange: false,
ExtraInfoFundamentals: false,
Sort: "",
},
})
m.Quotes = []Quote{
{
ResponseQuote: ResponseQuote{
Expand Down Expand Up @@ -641,14 +712,28 @@ var _ = Describe("Watchlist", func() {

When("no quotes are set", func() {
It("should render an empty watchlist", func() {
m := NewModel(false, false, false, "")
m := NewModel(c.Context{
Config: c.Config{
Separate: false,
ExtraInfoExchange: false,
ExtraInfoFundamentals: false,
Sort: "",
},
})
Expect(m.View()).To(Equal(""))
})
})

When("the window width is less than the minimum", func() {
It("should render an empty watchlist", func() {
m := NewModel(false, false, false, "")
m := NewModel(c.Context{
Config: c.Config{
Separate: false,
ExtraInfoExchange: false,
ExtraInfoFundamentals: false,
Sort: "",
},
})
m.Width = 70
Expect(m.View()).To(Equal("Terminal window too narrow to render content\nResize to fix (70/80)"))
})
Expand Down
2 changes: 1 addition & 1 deletion internal/ui/ui.go
Expand Up @@ -66,7 +66,7 @@ func NewModel(dep c.Dependencies, ctx c.Context) Model {
requestInterval: ctx.Config.RefreshInterval,
getQuotes: quote.GetQuotes(*dep.HttpClient, symbols),
getPositions: position.GetPositions(aggregatedLots),
watchlist: watchlist.NewModel(ctx.Config.Separate, ctx.Config.ExtraInfoExchange, ctx.Config.ExtraInfoFundamentals, ctx.Config.Sort),
watchlist: watchlist.NewModel(ctx),
summary: summary.NewModel(),
}
}
Expand Down

0 comments on commit 590ee57

Please sign in to comment.