Fixes unit tests (#1762)

This commit is contained in:
Mihovil Ilakovac 2024-02-14 11:13:25 +01:00 committed by GitHub
parent aeefab519d
commit 0d0deb47f1
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 26 additions and 16 deletions

View File

@ -22,7 +22,7 @@ spec_GeneratorJsImportTest = do
it "makes a JsImport from ExtImport" $ do
extImportToJsImport pathToExtCodeDir pathFromImportLocationToExtCodeDir extImport
`shouldBe` JI.JsImport
{ JI._path = [SP.relfileP|../ext-src/folder/test.js|],
{ JI._path = JI.RelativeImportPath [SP.relfileP|../ext-src/folder/test.js|],
JI._name = JsImportModule "test",
JI._importAlias = Nothing
}

View File

@ -1,6 +1,6 @@
module JsImportTest where
import StrongPath (Dir', File', Path, Posix, Rel, (</>))
import StrongPath ((</>))
import qualified StrongPath as SP
import Test.Tasty.Hspec (Spec, describe, it, shouldBe)
import Wasp.JsImport
@ -9,43 +9,53 @@ spec_JsImportTest :: Spec
spec_JsImportTest = do
describe "makeJsImport" $ do
it "makes JsImport with default import from a path" $ do
makeJsImport testJsFileImportPath (JsImportModule "test")
makeJsImport testRelativeImportPath (JsImportModule "test")
`shouldBe` JsImport
{ _path = testJsFileImportPath,
{ _path = testRelativeImportPath,
_name = JsImportModule "test",
_importAlias = Nothing
}
describe "applyJsImportAlias" $ do
it "applies alias to JsImport" $ do
let jsImport = makeJsImport testJsFileImportPath (JsImportModule "test")
let jsImport = makeJsImport testRelativeImportPath (JsImportModule "test")
applyJsImportAlias (Just "alias") jsImport
`shouldBe` jsImport {_importAlias = Just "alias"}
describe "getJsImportStmtAndIdentifier" $ do
describe "generates import statement and identifier from" $ do
it "default import" $ do
getJsImportStmtAndIdentifier
(makeJsImport testJsFileImportPath (JsImportModule "test"))
`shouldBe` ("import test from '" ++ generatedImportPathForTestJsFile ++ "'", "test")
(makeJsImport testRelativeImportPath (JsImportModule "test"))
`shouldBe` ("import test from '" ++ generatedImportPathForRelativeImportPath ++ "'", "test")
it "default import with alias" $ do
getJsImportStmtAndIdentifier
( applyJsImportAlias
(Just "alias")
(makeJsImport testJsFileImportPath (JsImportModule "test"))
(makeJsImport testRelativeImportPath (JsImportModule "test"))
)
`shouldBe` ("import alias from '" ++ generatedImportPathForTestJsFile ++ "'", "alias")
`shouldBe` ("import alias from '" ++ generatedImportPathForRelativeImportPath ++ "'", "alias")
it "named import" $ do
getJsImportStmtAndIdentifier
(makeJsImport testJsFileImportPath (JsImportField "test"))
`shouldBe` ("import { test } from '" ++ generatedImportPathForTestJsFile ++ "'", "test")
(makeJsImport testRelativeImportPath (JsImportField "test"))
`shouldBe` ("import { test } from '" ++ generatedImportPathForRelativeImportPath ++ "'", "test")
it "named import with alias" $ do
getJsImportStmtAndIdentifier
( applyJsImportAlias
(Just "alias")
(makeJsImport testJsFileImportPath (JsImportField "test"))
(makeJsImport testRelativeImportPath (JsImportField "test"))
)
`shouldBe` ("import { test as alias } from '" ++ generatedImportPathForTestJsFile ++ "'", "alias")
`shouldBe` ("import { test as alias } from '" ++ generatedImportPathForRelativeImportPath ++ "'", "alias")
it "import from module" $ do
getJsImportStmtAndIdentifier
(makeJsImport testModuleImportPath (JsImportModule "test"))
`shouldBe` ("import test from '" ++ generatedImportPathForModuleImportPath ++ "'", "test")
where
testJsFileImportPath :: Path Posix (Rel Dir') File'
testJsFileImportPath = [SP.reldirP|ext-src|] </> [SP.relfileP|folder/test.js|]
testRelativeImportPath :: JsImportPath
testRelativeImportPath = RelativeImportPath $ [SP.reldirP|ext-src|] </> [SP.relfileP|folder/test.js|]
generatedImportPathForTestJsFile = "./ext-src/folder/test.js"
generatedImportPathForRelativeImportPath = "./ext-src/folder/test.js"
testModuleImportPath :: JsImportPath
testModuleImportPath = ModuleImportPath [SP.relfileP|wasp/server/api|]
generatedImportPathForModuleImportPath :: String
generatedImportPathForModuleImportPath = "wasp/server/api"