44from typing import cast
55from urllib .parse import urlparse
66from pathlib import Path
7+ from unittest .mock import patch
8+
9+ from webapp .app import create_app
710from webapp .vite_integration .impl import (
811 ProdViteIntegration ,
912 DevViteIntegration ,
1013)
1114import webapp .vite_integration .exceptions as vite_exceptions
1215
13- MOCK_OUTPUT_PATH = "/tmp/python_vite_test"
16+
17+ MOCK_CONFIG = {
18+ "mode" : "development" ,
19+ "port" : 9999 ,
20+ "outdir" : "/tmp/python_vite_test" ,
21+ }
1422MOCK_ASSET_PATH = "test/path/for/asset.ts"
1523MOCK_SCSS_PATH = "test/path/for/styles.scss"
1624MOCK_MANIFEST = {
4149
4250class TestsDevViteIntegration (TestCase ):
4351 def setUp (self ):
44- self .vite = DevViteIntegration ()
45-
46- def tests_dev_tools (self ):
47- dev_tools = self .vite .get_dev_tools ()
48- assert "@vite/client" in dev_tools
49- assert "@react-refresh" in dev_tools
52+ self .vite = DevViteIntegration (MOCK_CONFIG )
5053
5154 def tests_get_asset_url (self ):
5255 url = self .vite .get_asset_url (MOCK_ASSET_PATH )
@@ -69,27 +72,24 @@ def tests_get_imported_css(self):
6972class TestsProdViteIntegration (TestCase ):
7073 def setUp (self ):
7174 # create a fake Vite output directory
72- manifest_path = Path (f" { MOCK_OUTPUT_PATH } /.vite/manifest.json" )
75+ manifest_path = Path (f' { MOCK_CONFIG [ "outdir" ] } /.vite/manifest.json' )
7376 manifest_path .parent .mkdir (exist_ok = True , parents = True )
7477 with manifest_path .open ("w+" ) as file :
7578 file .write (json .dumps (MOCK_MANIFEST ))
7679
7780 for entry in MOCK_MANIFEST .values ():
7881 file = cast (dict , entry ).get ("file" , "" )
79- file_path = Path (f" { MOCK_OUTPUT_PATH } /{ file } " )
82+ file_path = Path (f' { MOCK_CONFIG [ "outdir" ] } /{ file } ' )
8083 file_path .parent .mkdir (exist_ok = True , parents = True )
8184 with file_path .open ("w+" ) as file :
8285 file .write ("" )
8386
84- # inject the mocks in the static class scope before initalizing
85- ProdViteIntegration .OUT_DIR = MOCK_OUTPUT_PATH
86-
8787 def tearDown (self ):
88- rmtree (MOCK_OUTPUT_PATH )
88+ rmtree (MOCK_CONFIG [ "outdir" ] )
8989
9090 def tests_good_manifest_file (self ):
9191 # attempt to init
92- ProdViteIntegration ()
92+ ProdViteIntegration (MOCK_CONFIG )
9393
9494 def tests_bad_manifest_file (self ):
9595 # try to init a ProdViteIntegration instance with a bad manifest file
@@ -100,50 +100,44 @@ def tests_bad_manifest_file(self):
100100 ProdViteIntegration .BUILD_MANIFEST = "file/that/does/not/exist"
101101
102102 with self .assertRaises (vite_exceptions .ManifestPathException ):
103- self .vite = ProdViteIntegration ()
103+ self .vite = ProdViteIntegration (MOCK_CONFIG )
104104
105+ # reset build manifest path to previous value
105106 ProdViteIntegration .BUILD_MANIFEST = old_manifest_name
106107
107- def tests_dev_tools (self ):
108- vite = ProdViteIntegration ()
109- dev_tools = vite .get_dev_tools ()
110- assert dev_tools == ""
111-
112108 def tests_get_asset_url__bad_asset (self ):
113- vite = ProdViteIntegration ()
109+ vite = ProdViteIntegration (MOCK_CONFIG )
114110 with self .assertRaises (vite_exceptions .ManifestContentException ):
115111 vite .get_asset_url ("this_asset_does_not_exist.ts" )
116112
117113 def tests_get_asset_url__bad_path (self ):
118114 # try to load an asset declared in the manifest but without a real
119115 # file backing it
116+
120117 # load a proper manifest...
121118 ProdViteIntegration .manifest = MOCK_MANIFEST
122119 # but also load a broken OUT_DIR path
123- ProdViteIntegration .OUT_DIR = "/tmp/path/does/not/exist"
124-
125- vite = ProdViteIntegration ()
120+ vite = ProdViteIntegration ({"outdir" : "/tmp/path/does/not/exist" })
126121 with self .assertRaises (vite_exceptions .AssetPathException ):
127122 vite .get_asset_url (MOCK_ASSET_PATH )
128123
129124 # cleanup
130- ProdViteIntegration .OUT_DIR = MOCK_OUTPUT_PATH
131125 ProdViteIntegration .manifest = None
132126
133127 def tests_get_asset_url__is_not_ts (self ):
134- vite = ProdViteIntegration ()
128+ vite = ProdViteIntegration (MOCK_CONFIG )
135129 url = vite .get_asset_url (MOCK_ASSET_PATH )
136130 assert MOCK_ASSET_PATH not in url # source asset is a .ts file
137131 assert url .endswith (".js" ) # dist asset is a .js file
138132
139133 def tests_get_asset_url__is_not_scss (self ):
140- vite = ProdViteIntegration ()
134+ vite = ProdViteIntegration (MOCK_CONFIG )
141135 url = vite .get_asset_url (MOCK_SCSS_PATH )
142136 assert MOCK_SCSS_PATH not in url # source asset is a .scss file
143137 assert url .endswith (".css" ) # dist asset is a .css file
144138
145139 def tests_get_imported_chunks__bad_asset (self ):
146- vite = ProdViteIntegration ()
140+ vite = ProdViteIntegration (MOCK_CONFIG )
147141 with self .assertRaises (vite_exceptions .ManifestContentException ):
148142 vite .get_imported_chunks ("this_asset_does_not_exist.ts" )
149143
@@ -153,18 +147,15 @@ def tests_get_imported_chunks__bad_path(self):
153147 # load a proper manifest...
154148 ProdViteIntegration .manifest = MOCK_MANIFEST
155149 # but also load a broken OUT_DIR path
156- ProdViteIntegration .OUT_DIR = "/tmp/path/does/not/exist"
157-
158- vite = ProdViteIntegration ()
150+ vite = ProdViteIntegration ({"outdir" : "/tmp/path/does/not/exist" })
159151 with self .assertRaises (vite_exceptions .AssetPathException ):
160152 vite .get_imported_chunks (MOCK_ASSET_PATH )
161153
162154 # cleanup
163- ProdViteIntegration .OUT_DIR = MOCK_OUTPUT_PATH
164155 ProdViteIntegration .manifest = None
165156
166157 def tests_get_imported_chunks (self ):
167- vite = ProdViteIntegration ()
158+ vite = ProdViteIntegration (MOCK_CONFIG )
168159 js_entries = filter (
169160 lambda x : x ["file" ].endswith (".js" ), MOCK_MANIFEST .values ()
170161 )
@@ -173,5 +164,30 @@ def tests_get_imported_chunks(self):
173164 )
174165
175166 def tests_get_imported_css (self ):
176- vite = ProdViteIntegration ()
167+ vite = ProdViteIntegration (MOCK_CONFIG )
177168 assert len (vite .get_imported_css (MOCK_ASSET_PATH )) == 1
169+
170+
171+ class TestsFlaskViteExtension (TestCase ):
172+ @patch ("webapp.config.VITE_MODE" , "development" )
173+ @patch ("webapp.config.VITE_PORT" , MOCK_CONFIG ["port" ])
174+ @patch ("webapp.config.VITE_OUTDIR" , MOCK_CONFIG ["outdir" ])
175+ def setUp (self ):
176+ self .app = create_app (testing = True )
177+ self .client = self .app .test_client ()
178+
179+ def test_extension_init (self ):
180+ self .assertEqual (self .app .config .get ("VITE_MODE" ), "development" )
181+ self .assertEqual (self .app .config .get ("VITE_PORT" ), MOCK_CONFIG ["port" ])
182+ self .assertEqual (
183+ self .app .config .get ("VITE_OUTDIR" ), MOCK_CONFIG ["outdir" ]
184+ )
185+
186+ def test_dev_tools (self ):
187+ port = self .app .config .get ("VITE_PORT" )
188+ response = self .client .get ("/" )
189+ self .assertEqual (response .status_code , 200 )
190+
191+ body = response .get_data (as_text = True )
192+ self .assertGreater (len (body ), 0 )
193+ self .assertIn (f"http://localhost:{ port } /@vite/client" , body )
0 commit comments