Passed
Push — main ( ca1a65...aff05a )
by Acho
02:34
created

tracker_service_test.go   A

Size/Duplication

Total Lines 67
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 43
dl 0
loc 67
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A client.TestTrackerService_TrackEvent 0 28 1
A client.TestTrackerService_TrackEventWithError 0 24 1
1
package client
2
3
import (
4
	"context"
5
	"encoding/json"
6
	"net/http"
7
	"testing"
8
9
	"github.com/NdoleStudio/plunk-go/internal/helpers"
10
	"github.com/NdoleStudio/plunk-go/internal/stubs"
11
	"github.com/stretchr/testify/assert"
12
)
13
14
func TestTrackerService_TrackEvent(t *testing.T) {
15
	// Arrange
16
	apiKey := "test-api-key"
17
	request := new(http.Request)
18
	server := helpers.MakeRequestCapturingTestServer(http.StatusOK, stubs.TrackEventResponse(), request)
19
	client := New(WithBaseURL(server.URL), WithPublicKey(apiKey))
20
21
	// Act
22
	event, response, err := client.Tracker.TrackEvent(context.Background(), &TrackEventRequest{
23
		Email:      "[email protected]",
24
		Event:      "purchase",
25
		Subscribed: true,
26
		Data: map[string]any{
27
			"firstName": "John",
28
			"lastName":  "Doe",
29
			"plan":      "premium",
30
		},
31
	})
32
33
	// Assert
34
	assert.Nil(t, err)
35
	assert.Equal(t, http.StatusOK, response.HTTPResponse.StatusCode)
36
	assert.Equal(t, "Bearer "+apiKey, request.Header.Get("Authorization"))
37
	jsonContent, _ := json.Marshal(event)
38
	assert.JSONEq(t, string(stubs.TrackEventResponse()), string(jsonContent))
39
40
	// Teardown
41
	server.Close()
42
}
43
44
func TestTrackerService_TrackEventWithError(t *testing.T) {
45
	// Arrange
46
	apiKey := "test-api-key"
47
	server := helpers.MakeTestServer(http.StatusInternalServerError, nil)
48
	client := New(WithBaseURL(server.URL), WithPublicKey(apiKey))
49
50
	// Act
51
	_, response, err := client.Tracker.TrackEvent(context.Background(), &TrackEventRequest{
52
		Email:      "[email protected]",
53
		Event:      "purchase",
54
		Subscribed: true,
55
		Data: map[string]any{
56
			"firstName": "John",
57
			"lastName":  "Doe",
58
			"plan":      "premium",
59
		},
60
	})
61
62
	// Assert
63
	assert.NotNil(t, err)
64
	assert.Equal(t, http.StatusInternalServerError, response.HTTPResponse.StatusCode)
65
66
	// Teardown
67
	server.Close()
68
}
69