Skip to content

Commit

Permalink
inertia v2: clearHistory and encryptHistory tests
Browse files Browse the repository at this point in the history
  • Loading branch information
romsar committed Oct 10, 2024
1 parent 90b6170 commit df27c8a
Show file tree
Hide file tree
Showing 6 changed files with 278 additions and 0 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -322,12 +322,16 @@ func TestHomepage(t *testing.T) {
assertable.AssertVersion("foo bar")
assertable.AssertURL("https://example.com")
assertable.AssertProps(inertia.Props{"foo": "bar"})
assertable.AssertEncryptHistory(true)
assertable.AssertClearHistory(true)

// or work with the data yourself:
assertable.Component // Foo/Bar
assertable.Version // foo bar
assertable.URL // https://example.com
assertable.Props // inertia.Props{"foo": "bar"}
assertable.EncryptHistory // true
assertable.ClearHistory // false
assertable.Body // full response body
}
```
Expand Down
124 changes: 124 additions & 0 deletions context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -418,3 +418,127 @@ func Test_ValidationErrorsFromContext(t *testing.T) {
})
}
}

func TestInertia_SetEncryptHistory(t *testing.T) {
t.Parallel()

ctx := SetEncryptHistory(context.Background())

got, ok := ctx.Value(encryptHistoryContextKey).(bool)
if !ok {
t.Fatal("encrypt history from context is not `bool` type")
}

want := true

if got != want {
t.Fatalf("encryptHistory=%t, want=%t", got, want)
}
}

func Test_EncryptHistoryFromContext(t *testing.T) {
t.Parallel()

tests := []struct {
name string
ctxData any
want bool
}{
{
name: "nil",
ctxData: nil,
want: false,
},
{
name: "false",
ctxData: false,
want: false,
},
{
name: "true",
ctxData: true,
want: true,
},
{
name: "wrong type",
ctxData: []string{"foo", "bar"},
want: false,
},
}
for _, tt := range tests {
tt := tt

t.Run(tt.name, func(t *testing.T) {
t.Parallel()

ctx := context.WithValue(context.Background(), encryptHistoryContextKey, tt.ctxData)

got, _ := EncryptHistoryFromContext(ctx)
if got != tt.want {
t.Fatalf("encryptHistory=%t, want=%t", got, tt.want)
}
})
}
}

func TestInertia_SetClearHistory(t *testing.T) {
t.Parallel()

ctx := SetClearHistory(context.Background())

got, ok := ctx.Value(clearHistoryContextKey).(bool)
if !ok {
t.Fatal("clear history from context is not `bool` type")
}

want := true

if got != want {
t.Fatalf("clearHistory=%t, want=%t", got, want)
}
}

func Test_ClearHistoryFromContext(t *testing.T) {
t.Parallel()

tests := []struct {
name string
ctxData any
want bool
}{
{
name: "nil",
ctxData: nil,
want: false,
},
{
name: "false",
ctxData: false,
want: false,
},
{
name: "true",
ctxData: true,
want: true,
},
{
name: "wrong type",
ctxData: []string{"foo", "bar"},
want: false,
},
}
for _, tt := range tests {
tt := tt

t.Run(tt.name, func(t *testing.T) {
t.Parallel()

ctx := context.WithValue(context.Background(), clearHistoryContextKey, tt.ctxData)

got := ClearHistoryFromContext(ctx)
if got != tt.want {
t.Fatalf("clearHistory=%t, want=%t", got, tt.want)
}
})
}
}
16 changes: 16 additions & 0 deletions option_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -252,3 +252,19 @@ func TestWithFlashProvider(t *testing.T) {
t.Fatalf("flash provider=%v, want=%s", i.flash, want)
}
}

func TestWithEncryptHistory(t *testing.T) {
t.Parallel()

i := I()

option := WithEncryptHistory(true)

if err := option(i); err != nil {
t.Fatalf("unexpected error: %s", err)
}

if !i.encryptHistory {
t.Fatalf("encryptHistory=%t, want=%t", i.encryptHistory, true)
}
}
22 changes: 22 additions & 0 deletions response_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,8 @@ func TestInertia_Render(t *testing.T) {
assertable.AssertProps(Props{"foo": "bar", "errors": map[string]any{}})
assertable.AssertVersion("f8v01xv4h4")
assertable.AssertURL("/home")
assertable.AssertEncryptHistory(false)
assertable.AssertEncryptHistory(false)

assertInertiaResponse(t, w)
assertJSONResponse(t, w)
Expand Down Expand Up @@ -272,6 +274,26 @@ func TestInertia_Render(t *testing.T) {
})
})

t.Run("history encryption", func(t *testing.T) {
t.Parallel()

w, r := requestMock(http.MethodGet, "/home")
asInertiaRequest(r)

ctx := r.Context()
ctx = SetEncryptHistory(ctx, true)
ctx = SetClearHistory(ctx)

err := I().Render(w, r.WithContext(ctx), "Some/Component")
if err != nil {
t.Fatalf("unexpected error: %s", err)
}

assertable := AssertFromString(t, w.Body.String())
assertable.AssertEncryptHistory(true)
assertable.AssertClearHistory(true)
})

t.Run("props value resolving", func(t *testing.T) {
t.Parallel()

Expand Down
20 changes: 20 additions & 0 deletions testing.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,26 @@ func (i AssertableInertia) AssertProps(want Props) {
}
}

// AssertEncryptHistory verifies that encrypt history
// value from Inertia response and the passed value are the same.
func (i AssertableInertia) AssertEncryptHistory(want bool) {
i.t.Helper()

if i.EncryptHistory != want {
i.t.Fatalf("inertia: EncryptHistory=%t, want=%t", i.EncryptHistory, want)
}
}

// AssertClearHistory verifies that clear history
// value from Inertia response and the passed value are the same.
func (i AssertableInertia) AssertClearHistory(want bool) {
i.t.Helper()

if i.ClearHistory != want {
i.t.Fatalf("inertia: ClearHistory=%t, want=%t", i.ClearHistory, want)
}
}

var containerRe = regexp.MustCompile(` data-page="(.*?)"`)

// AssertFromReader creates AssertableInertia from the io.Reader body.
Expand Down
92 changes: 92 additions & 0 deletions testing_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,98 @@ func TestAssertableInertia_AssertProps(t *testing.T) {
})
}

func TestAssertableInertia_EncryptHistory(t *testing.T) {
t.Parallel()

t.Run("positive", func(t *testing.T) {
t.Parallel()

mock := new(tMock)

i := AssertableInertia{
t: mock,
page: &page{EncryptHistory: true},
}

i.AssertEncryptHistory(true)

if !mock.helperInvoked {
t.Fatal("expected Helper() to be invoked")
}

if mock.isFailed {
t.Fatal("unexpected assertion failure")
}
})

t.Run("negative", func(t *testing.T) {
t.Parallel()

mock := new(tMock)

i := AssertableInertia{
page: &page{EncryptHistory: true},
t: mock,
}

i.AssertEncryptHistory(false)

if !mock.helperInvoked {
t.Fatal("expected Helper() to be invoked")
}

if !mock.isFailed {
t.Fatal("expected assertion failure")
}
})
}

func TestAssertableInertia_ClearHistory(t *testing.T) {
t.Parallel()

t.Run("positive", func(t *testing.T) {
t.Parallel()

mock := new(tMock)

i := AssertableInertia{
t: mock,
page: &page{ClearHistory: true},
}

i.AssertClearHistory(true)

if !mock.helperInvoked {
t.Fatal("expected Helper() to be invoked")
}

if mock.isFailed {
t.Fatal("unexpected assertion failure")
}
})

t.Run("negative", func(t *testing.T) {
t.Parallel()

mock := new(tMock)

i := AssertableInertia{
page: &page{ClearHistory: true},
t: mock,
}

i.AssertClearHistory(false)

if !mock.helperInvoked {
t.Fatal("expected Helper() to be invoked")
}

if !mock.isFailed {
t.Fatal("expected assertion failure")
}
})
}

func TestAssertFromString(t *testing.T) {
t.Parallel()

Expand Down

0 comments on commit df27c8a

Please sign in to comment.