Skip to content

Commit

Permalink
Fix view generator tests.
Browse files Browse the repository at this point in the history
- Tests were enforcing incorrect Haskell code generation for `genericView`
- Update tests to use `tableNameToViewName` like controller tests.
- Add tests view names with underscores and camel case.
  • Loading branch information
liammcdermott committed Oct 9, 2024
1 parent db98a1f commit 84f2cbd
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 5 deletions.
43 changes: 39 additions & 4 deletions Test/IDE/CodeGeneration/ViewGenerator.hs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ tests = do
}
]
it "should build a view with name \"EditView\"" do
let viewName = "EditView"
let rawViewName = "EditView"
let viewName = tableNameToViewName rawViewName
let rawControllerName = "Pages"
let controllerName = tableNameToControllerName rawControllerName
let modelName = tableNameToModelName rawControllerName
Expand All @@ -50,8 +51,41 @@ tests = do



it "should build a view with name \"edit_view\"" do
let rawViewName = "edit_view"
let viewName = tableNameToViewName rawViewName
let rawControllerName = "Pages"
let controllerName = tableNameToControllerName rawControllerName
let modelName = tableNameToModelName rawControllerName
let applicationName = "Web"
let paginationEnabled = False
let config = ViewGenerator.ViewConfig { .. }
let builtPlan = ViewGenerator.buildPlan' schema config

builtPlan `shouldBe`
[ EnsureDirectory {directory = "Web/View/Pages"},CreateFile {filePath = "Web/View/Pages/Edit.hs", fileContent = "module Web.View.Pages.Edit where\nimport Web.View.Prelude\n\ndata EditView = EditView { page :: Page }\n\ninstance View EditView where\n html EditView { .. } = [hsx|\n {breadcrumb}\n <h1>Edit Page</h1>\n {renderForm page}\n |]\n where\n breadcrumb = renderBreadcrumb\n [ breadcrumbLink \"Pages\" PagesAction\n , breadcrumbText \"Edit Page\"\n ]\n\nrenderForm :: Page -> Html\nrenderForm page = formFor page [hsx|\n \n {submitButton}\n\n|]"},AddImport {filePath = "Web/Controller/Pages.hs", fileContent = "import Web.View.Pages.Edit"}
]


it "should build a view with name \"editView\"" do
let rawViewName = "editView"
let viewName = tableNameToViewName rawViewName
let rawControllerName = "Pages"
let controllerName = tableNameToControllerName rawControllerName
let modelName = tableNameToModelName rawControllerName
let applicationName = "Web"
let paginationEnabled = False
let config = ViewGenerator.ViewConfig { .. }
let builtPlan = ViewGenerator.buildPlan' schema config

builtPlan `shouldBe`
[ EnsureDirectory {directory = "Web/View/Pages"},CreateFile {filePath = "Web/View/Pages/Edit.hs", fileContent = "module Web.View.Pages.Edit where\nimport Web.View.Prelude\n\ndata EditView = EditView { page :: Page }\n\ninstance View EditView where\n html EditView { .. } = [hsx|\n {breadcrumb}\n <h1>Edit Page</h1>\n {renderForm page}\n |]\n where\n breadcrumb = renderBreadcrumb\n [ breadcrumbLink \"Pages\" PagesAction\n , breadcrumbText \"Edit Page\"\n ]\n\nrenderForm :: Page -> Html\nrenderForm page = formFor page [hsx|\n \n {submitButton}\n\n|]"},AddImport {filePath = "Web/Controller/Pages.hs", fileContent = "import Web.View.Pages.Edit"}
]


it "should build a view with name \"Edit\"" do
let viewName = "Edit"
let rawViewName = "Edit"
let viewName = tableNameToViewName rawViewName
let rawControllerName = "Pages"
let controllerName = tableNameToControllerName rawControllerName
let modelName = tableNameToModelName rawControllerName
Expand All @@ -66,7 +100,8 @@ tests = do


it "should build a view with name \"Test\"" do
let viewName = "Test"
let rawViewName = "Test"
let viewName = tableNameToViewName rawViewName
let rawControllerName = "Pages"
let controllerName = tableNameToControllerName rawControllerName
let modelName = tableNameToModelName rawControllerName
Expand All @@ -76,5 +111,5 @@ tests = do
let builtPlan = ViewGenerator.buildPlan' schema config

builtPlan `shouldBe`
[ EnsureDirectory {directory = "Web/View/Pages"},CreateFile {filePath = "Web/View/Pages/Test.hs", fileContent = "module Web.View.Pages.Test where\nimport Web.View.Prelude\ndata TestView = {TestView}\n\ninstance View TestView where\n html TestView { .. } = [hsx|\n {breadcrumb}\n <h1>TestView</h1>\n |]\n where\n breadcrumb = renderBreadcrumb\n [ breadcrumbLink \"Tests\" PagesAction\n , breadcrumbText \"TestView\"\n ]"},AddImport {filePath = "Web/Controller/Pages.hs", fileContent = "import Web.View.Pages.Test"}
[ EnsureDirectory {directory = "Web/View/Pages"},CreateFile {filePath = "Web/View/Pages/Test.hs", fileContent = "module Web.View.Pages.Test where\nimport Web.View.Prelude\ndata TestView = TestView\n\ninstance View TestView where\n html TestView { .. } = [hsx|\n {breadcrumb}\n <h1>TestView</h1>\n |]\n where\n breadcrumb = renderBreadcrumb\n [ breadcrumbLink \"Tests\" PagesAction\n , breadcrumbText \"TestView\"\n ]"},AddImport {filePath = "Web/Controller/Pages.hs", fileContent = "import Web.View.Pages.Test"}
]
2 changes: 1 addition & 1 deletion ihp-ide/IHP/IDE/CodeGen/ViewGenerator.hs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ buildPlan' schema config =

genericView = [trimming|
${viewHeader}
data ${nameWithSuffix} = {${nameWithSuffix}}
data ${nameWithSuffix} = ${nameWithSuffix}

instance View ${nameWithSuffix} where
html ${nameWithSuffix} { .. } = [hsx|
Expand Down

0 comments on commit 84f2cbd

Please sign in to comment.