Skip to content

Commit

Permalink
Fix tests, add home page, show alert if log in failed, fix login and …
Browse files Browse the repository at this point in the history
…registration flow
  • Loading branch information
wnederhof committed Mar 15, 2024
1 parent 5d25b37 commit c60fae6
Show file tree
Hide file tree
Showing 29 changed files with 286 additions and 354 deletions.
16 changes: 8 additions & 8 deletions internal/cli/arg_parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,19 @@ import (
"strings"
)

func parseArgs(cliArgs cli.Args) (generator.Model, error) {
func parseArgs(cliArgs cli.Args, minArgs int) (generator.Model, error) {
args := make([]string, cliArgs.Len())
for i := 0; i < cliArgs.Len(); i++ {
args[i] = cliArgs.Get(i)
}
return parseArgsFromStrings(args)
return parseArgsFromStrings(args, minArgs)
}

func parseArgsFromStrings(args []string) (generator.Model, error) {
func parseArgsFromStrings(args []string, minArgs int) (generator.Model, error) {
if len(args) < 2 {
return generator.Model{}, errors.New("usage: <name> (<fieldName>:<fieldType>)+")
}
attributes, err := parseAttributeArgsFromStrings(args[1:])
attributes, err := parseAttributeArgsFromStrings(args[1:], minArgs)
if err != nil {
return generator.Model{}, err
}
Expand All @@ -29,17 +29,17 @@ func parseArgsFromStrings(args []string) (generator.Model, error) {
}, nil
}

func parseAttributeArgs(cliArgs cli.Args) ([]generator.ModelAttribute, error) {
func parseAttributeArgs(cliArgs cli.Args, minArgs int) ([]generator.ModelAttribute, error) {
// TODO test separately
args := make([]string, cliArgs.Len())
for i := 0; i < cliArgs.Len(); i++ {
args[i] = cliArgs.Get(i)
}
return parseAttributeArgsFromStrings(args)
return parseAttributeArgsFromStrings(args, minArgs)
}

func parseAttributeArgsFromStrings(attributeArgs []string) ([]generator.ModelAttribute, error) {
if len(attributeArgs) < 1 {
func parseAttributeArgsFromStrings(attributeArgs []string, minArgs int) ([]generator.ModelAttribute, error) {
if len(attributeArgs) < minArgs {
return []generator.ModelAttribute{}, errors.New("usage: (<fieldName>:<fieldType>)+")
}
attributeCount := len(attributeArgs)
Expand Down
18 changes: 9 additions & 9 deletions internal/cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func Run(args []string) error {
Aliases: []string{"bes"},
Usage: "Backend Scaffold",
Action: func(c *cli.Context) error {
model, err := parseArgs(c.Args())
model, err := parseArgs(c.Args(), 1)
if err != nil {
return err
}
Expand All @@ -55,7 +55,7 @@ func Run(args []string) error {
Aliases: []string{"bem"},
Usage: "Model files, including migration script, entity and repository",
Action: func(c *cli.Context) error {
model, err := parseArgs(c.Args())
model, err := parseArgs(c.Args(), 1)
if err != nil {
return err
}
Expand All @@ -67,7 +67,7 @@ func Run(args []string) error {
Aliases: []string{"bea"},
Usage: "GraphQL API (schema and resolvers)",
Action: func(c *cli.Context) error {
model, err := parseArgs(c.Args())
model, err := parseArgs(c.Args(), 1)
if err != nil {
return err
}
Expand All @@ -79,7 +79,7 @@ func Run(args []string) error {
Aliases: []string{"bsv"},
Usage: "Service between API and repository",
Action: func(c *cli.Context) error {
model, err := parseArgs(c.Args())
model, err := parseArgs(c.Args(), 1)
if err != nil {
return err
}
Expand All @@ -102,7 +102,7 @@ func Run(args []string) error {
Aliases: []string{"fes"},
Usage: "Frontend Scaffold (Generate frontend support first)",
Action: func(c *cli.Context) error {
model, err := parseArgs(c.Args())
model, err := parseArgs(c.Args(), 1)
if err != nil {
return err
}
Expand All @@ -114,7 +114,7 @@ func Run(args []string) error {
Aliases: []string{"s"},
Usage: "Backend and Frontend Scaffold (Generate frontend support first)",
Action: func(c *cli.Context) error {
model, err := parseArgs(c.Args())
model, err := parseArgs(c.Args(), 1)
if err != nil {
return err
}
Expand All @@ -126,7 +126,7 @@ func Run(args []string) error {
Aliases: []string{"ba"},
Usage: "Backend Authentication - EXPERIMENTAL",
Action: func(c *cli.Context) error {
model, err := parseAttributeArgs(c.Args())
model, err := parseAttributeArgs(c.Args(), 0)
if err != nil {
return err
}
Expand All @@ -138,7 +138,7 @@ func Run(args []string) error {
Aliases: []string{"fa"},
Usage: "Frontend Authentication - EXPERIMENTAL",
Action: func(c *cli.Context) error {
model, err := parseAttributeArgs(c.Args())
model, err := parseAttributeArgs(c.Args(), 0)
if err != nil {
return err
}
Expand All @@ -150,7 +150,7 @@ func Run(args []string) error {
Aliases: []string{"a"},
Usage: "Authentication - EXPERIMENTAL",
Action: func(c *cli.Context) error {
model, err := parseAttributeArgs(c.Args())
model, err := parseAttributeArgs(c.Args(), 0)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/generator/attributes.go
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,7 @@ func provideReactTemplateExpectedContextAttributes(context map[string]interface{
case NULL_BOOLEAN:
context["fieldFrontendExpectedValue"] = "true"
case RELATIONAL:
context["fieldFrontendExpectedValue"] = "1"
context["fieldFrontendExpectedValue"] = "'1'"
default:
panic("Undetermined attribute type.")
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/generator/attributes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ func TestProvideFieldNameContextAttributes(t *testing.T) {
assert.Equal(t, "'Some streetName'", fields[7]["fieldFrontendExpectedValue"])
assert.Equal(t, "'2000-01-01'", fields[8]["fieldFrontendExpectedValue"])
assert.Equal(t, "true", fields[9]["fieldFrontendExpectedValue"])
assert.Equal(t, "1", fields[10]["fieldFrontendExpectedValue"])
assert.Equal(t, "'1'", fields[10]["fieldFrontendExpectedValue"])

// fieldFrontendTestValue
assert.Equal(t, "'Some streetName'", fields[0]["fieldFrontendTestValue"])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,98 +9,58 @@ describe('Login', () => {
{{field.fieldNameCamelCase}}: {{field.fieldFrontendTestValue}}{%endfor%}
}

it('renders form containing the necessary fields', () => {
it('has a header', () => {
const mockClient: Partial<Client> = {
executeQuery: jest.fn(),
executeMutation: jest.fn(),
executeSubscription: jest.fn(),
}
render(
<Provider value={mockClient as Client}>
<Login onSave={jest.fn()} onCancel={jest.fn()} />
</Provider>
){%for field in fields%}
expect(screen.queryByText('{{ field.fieldNamePascalCase }}')).not.toBeNull(){%endfor%}
})

it('renders form containing existing data if id is passed', () => {
const executeQuery = jest.fn()
executeQuery.mockReturnValue(
fromValue({
data: {
user{%for field in fields%}{%if field.isFieldRelational%},
{{field.fieldTypePluralCamelCase}}: [{ id: 1 }]{%endif%}{%endfor%}
}
})
)
const mockClient: Partial<Client> = {
executeQuery,
executeMutation: jest.fn(),
executeSubscription: jest.fn(),
}
render(
<Provider value={mockClient as Client}>
<Login id="1" onSave={jest.fn()} onCancel={jest.fn()} />
<Login />
</Provider>
)
const expectedFormValues = {
...user,
id: undefined{%for field in fields%}{%if field.isFieldRelational%},
{{field.fieldNameCamelCase}}: '1'{%endif%}{%endfor%}
}
delete expectedFormValues.id
expect(screen.getByRole('form')).toHaveFormValues(expectedFormValues)
expect(screen.queryAllByText('Login Form')).toHaveLength(1)
})

it('calls a mutation if Save is clicked if there is an id', async () => {
const executeQuery = jest.fn()
const executeMutation = jest.fn()
executeQuery.mockReturnValue(
fromValue({
data: { user },
}),
)
executeMutation.mockReturnValue(
fromValue({
data: {},
})
)
it('renders form containing the necessary fields', () => {
const mockClient: Partial<Client> = {
executeQuery,
executeMutation,
executeQuery: jest.fn(),
executeMutation: jest.fn(),
executeSubscription: jest.fn(),
}
const saveFn = jest.fn()
render(
<Provider value={mockClient as Client}>
<Login id="1" onSave={saveFn} onCancel={jest.fn()} />
<Login onLogin={jest.fn()} onCancel={jest.fn()} />
</Provider>
)
fireEvent.click(screen.getByText('Save'))
await waitFor(() => expect(mockClient.executeMutation).toBeCalled())
await waitFor(() => expect(saveFn).toBeCalled())
){%for field in fields%}
expect(screen.queryByText('{{ field.fieldNamePascalCase }}')).not.toBeNull(){%endfor%}
})

it('calls a mutation if Save is clicked if there is no id', async () => {
it('calls a mutation if Login is clicked', async () => {
const executeMutation = jest.fn()
executeMutation.mockReturnValue(
fromValue({
data: {},
data: {
login: {
id: 1
}
}
})
)
const mockClient: Partial<Client> = {
executeQuery: jest.fn(),
executeMutation,
executeSubscription: jest.fn(),
}
const saveFn = jest.fn()
const loginFn = jest.fn()
render(
<Provider value={mockClient as Client}>
<Form onSave={saveFn} onCancel={jest.fn()} />
<Login onLogin={loginFn} onCancel={jest.fn()} />
</Provider>
)
fireEvent.click(screen.getByText('Save'))
fireEvent.click(screen.getByText('Login'))
await waitFor(() => expect(mockClient.executeMutation).toBeCalled())
await waitFor(() => expect(saveFn).toBeCalled())
await waitFor(() => expect(loginFn).toBeCalled())
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -9,98 +9,54 @@ describe('Register', () => {
{{field.fieldNameCamelCase}}: {{field.fieldFrontendTestValue}}{%endfor%}
}

it('renders form containing the necessary fields', () => {
it('has a header', () => {
const mockClient: Partial<Client> = {
executeQuery: jest.fn(),
executeMutation: jest.fn(),
executeSubscription: jest.fn(),
}
render(
<Provider value={mockClient as Client}>
<Register onSave={jest.fn()} onCancel={jest.fn()} />
</Provider>
){%for field in fields%}
expect(screen.queryByText('{{ field.fieldNamePascalCase }}')).not.toBeNull(){%endfor%}
})

it('renders form containing existing data if id is passed', () => {
const executeQuery = jest.fn()
executeQuery.mockReturnValue(
fromValue({
data: {
user{%for field in fields%}{%if field.isFieldRelational%},
{{field.fieldTypePluralCamelCase}}: [{ id: 1 }]{%endif%}{%endfor%}
}
})
)
const mockClient: Partial<Client> = {
executeQuery,
executeMutation: jest.fn(),
executeSubscription: jest.fn(),
}
render(
<Provider value={mockClient as Client}>
<Register id="1" onSave={jest.fn()} onCancel={jest.fn()} />
<Register />
</Provider>
)
const expectedFormValues = {
...user,
id: undefined{%for field in fields%}{%if field.isFieldRelational%},
{{field.fieldNameCamelCase}}: '1'{%endif%}{%endfor%}
}
delete expectedFormValues.id
expect(screen.getByRole('form')).toHaveFormValues(expectedFormValues)
expect(screen.queryAllByText('Registration Form')).toHaveLength(1)
})

it('calls a mutation if Save is clicked if there is an id', async () => {
const executeQuery = jest.fn()
const executeMutation = jest.fn()
executeQuery.mockReturnValue(
fromValue({
data: { user },
}),
)
executeMutation.mockReturnValue(
fromValue({
data: {},
})
)
it('renders form containing the necessary fields', () => {
const mockClient: Partial<Client> = {
executeQuery,
executeMutation,
executeQuery: jest.fn(),
executeMutation: jest.fn(),
executeSubscription: jest.fn(),
}
const saveFn = jest.fn()
render(
<Provider value={mockClient as Client}>
<Register id="1" onSave={saveFn} onCancel={jest.fn()} />
<Register onRegister={jest.fn()} onCancel={jest.fn()} />
</Provider>
)
fireEvent.click(screen.getByText('Save'))
await waitFor(() => expect(mockClient.executeMutation).toBeCalled())
await waitFor(() => expect(saveFn).toBeCalled())
){%for field in fields%}
expect(screen.queryByText('{{ field.fieldNamePascalCase }}')).not.toBeNull(){%endfor%}
})

it('calls a mutation if Save is clicked if there is no id', async () => {
it('calls a mutation if Register is clicked', async () => {
const executeMutation = jest.fn()
executeMutation.mockReturnValue(
fromValue({
data: {},
data: {}
})
)
const mockClient: Partial<Client> = {
executeQuery: jest.fn(),
executeMutation,
executeSubscription: jest.fn(),
}
const saveFn = jest.fn()
const registerFn = jest.fn()
render(
<Provider value={mockClient as Client}>
<Register onSave={saveFn} onCancel={jest.fn()} />
<Register onRegister={registerFn} onCancel={jest.fn()} />
</Provider>
)
fireEvent.click(screen.getByText('Save'))
fireEvent.click(screen.getByText('Register'))
await waitFor(() => expect(mockClient.executeMutation).toBeCalled())
await waitFor(() => expect(saveFn).toBeCalled())
await waitFor(() => expect(registerFn).toBeCalled())
})
})

This file was deleted.

Loading

0 comments on commit c60fae6

Please sign in to comment.