generated from alexstojda/go-react-app
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
afe6ecd
commit 7d18928
Showing
5 changed files
with
151 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package api_test | ||
|
||
import ( | ||
jwt "github.com/appleboy/gin-jwt/v2" | ||
"gorm.io/gorm" | ||
"pinman/internal/app/api" | ||
"testing" | ||
|
||
"github.com/onsi/ginkgo/v2" | ||
"github.com/onsi/gomega" | ||
) | ||
|
||
func TestApi(t *testing.T) { | ||
gomega.RegisterFailHandler(ginkgo.Fail) | ||
ginkgo.RunSpecs(t, "Api Suite") | ||
} | ||
|
||
var _ = ginkgo.Describe("NewServer", func() { | ||
ginkgo.It("should not return nil", func() { | ||
server := api.NewServer( | ||
&gorm.DB{}, | ||
&jwt.GinJWTMiddleware{}, | ||
) | ||
gomega.Expect(server).NotTo(gomega.BeNil()) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package utils | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"reflect" | ||
) | ||
|
||
func CheckFieldsForNil(s any) error { | ||
var serverValue reflect.Value | ||
if reflect.TypeOf(s).Kind() == reflect.Ptr { | ||
serverValue = reflect.ValueOf(s).Elem() | ||
} else { | ||
serverValue = reflect.ValueOf(s) | ||
} | ||
for i := 0; i < serverValue.NumField(); i++ { | ||
fieldValue := serverValue.Field(i) | ||
if fieldValue.Kind() == reflect.Ptr && fieldValue.IsNil() { | ||
return errors.New(fmt.Sprint("field ", serverValue.Type().Field(i).Name, " is nil")) | ||
} | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package utils_test | ||
|
||
import ( | ||
"github.com/onsi/ginkgo/v2" | ||
"github.com/onsi/gomega" | ||
"pinman/internal/utils" | ||
) | ||
|
||
type TestStruct struct { | ||
Field1 *string | ||
Field2 *string | ||
Field3 *string | ||
} | ||
|
||
var _ = ginkgo.Describe("CheckFieldsForNil", func() { | ||
ginkgo.When("given a struct with all fields set", func() { | ||
ginkgo.It("should return nil", func() { | ||
testStruct := TestStruct{ | ||
Field1: utils.PtrString(""), | ||
Field2: utils.PtrString(""), | ||
Field3: utils.PtrString(""), | ||
} | ||
err := utils.CheckFieldsForNil(testStruct) | ||
gomega.Expect(err).To(gomega.BeNil()) | ||
}) | ||
}) | ||
ginkgo.When("given a struct with one field set", func() { | ||
ginkgo.It("should return an error", func() { | ||
testStruct := TestStruct{ | ||
Field1: utils.PtrString(""), | ||
Field2: nil, | ||
Field3: nil, | ||
} | ||
err := utils.CheckFieldsForNil(testStruct) | ||
gomega.Expect(err).NotTo(gomega.BeNil()) | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import axios from "axios"; | ||
import {Api} from "./pinballMap"; | ||
|
||
jest.mock('axios'); | ||
const mockedAxios = axios as jest.Mocked<typeof axios>; | ||
|
||
beforeEach(() => { | ||
mockedAxios.get.mockReset() | ||
}) | ||
|
||
describe('Api.getLocations()', () => { | ||
it('returns locations if api call was successful', async () => { | ||
const locations = [{ | ||
id: 1, | ||
name: 'Location 1', | ||
street: '123 Main St', | ||
city: 'Anytown', | ||
state: 'CA', | ||
zip: '12345', | ||
country: 'USA', | ||
num_machines: 5 | ||
}] | ||
mockedAxios.get.mockResolvedValueOnce({data: {locations: locations}}) | ||
|
||
const result = await new Api().locationsGet('Location 1') | ||
expect(mockedAxios.get).toBeCalledTimes(1) | ||
expect(mockedAxios.get).toBeCalledWith( | ||
'https://pinballmap.com/api/v1/locations.json?by_location_name=Location%201' | ||
) | ||
expect(result).toEqual(locations) | ||
}) | ||
}) | ||
|
||
describe('Api.parseError()', () => { | ||
it('returns error response if error is axios error', () => { | ||
const errorResponse = { | ||
errors: 'Error message' | ||
} | ||
const axiosError = { | ||
isAxiosError: true, | ||
response: { | ||
data: errorResponse | ||
} | ||
} | ||
const result = new Api().parseError(axiosError as any) | ||
expect(result).toEqual(errorResponse) | ||
}) | ||
it('throws error if error is not axios error', () => { | ||
const errorResponse = { | ||
errors: 'Error message' | ||
} | ||
const axiosError = { | ||
isAxiosError: false, | ||
response: { | ||
data: errorResponse | ||
} | ||
} | ||
expect(() => { | ||
new Api().parseError(axiosError as any) | ||
}).toThrowError() | ||
}) | ||
}) |