AbstractUpdateImports implements Runnable { @Override public void run() { List<String> lines = new ArrayList<>(); lines.addAll(getExportLines()); lines.addAll(getThemeLines()); lines.addAll(getCssLines()); collectModules(lines); writeImportLines(lines); } AbstractUpdateImports(File frontendDirectory, File npmDirectory,
            File generatedDirectory, File tokenFile); @Override void run();  }
@Test public void importsFilesAreNotFound_throws() { deleteExpectedImports(frontendDirectory, nodeModulesPath); exception.expect(IllegalStateException.class); updater.run(); }
@Test public void getModuleLines_npmPackagesDontExist_logExplanation() { useMockLog = true; Mockito.when(logger.isInfoEnabled()).thenReturn(true); boolean atLeastOneRemoved = false; for (String imprt : getExpectedImports()) { if (imprt.startsWith("@vaadin") && imprt.endsWith(".js")) { assertTrue(resolveImportFile(nodeModulesPath, nodeModulesPath, imprt).delete()); atLeastOneRemoved = true; } } assertTrue(atLeastOneRemoved); updater.run(); ArgumentCaptor<String> captor = ArgumentCaptor.forClass(String.class); Mockito.verify(logger).info(captor.capture()); Assert.assertThat(captor.getValue(), CoreMatchers.allOf( CoreMatchers.containsString( "@vaadin/vaadin-lumo-styles/spacing.js"), CoreMatchers.containsString(ERROR_MSG))); }
@Test public void getModuleLines_oneFrontendDependencyDoesntExist_throwExceptionAndlogExplanation() { useMockLog = true; Mockito.when(logger.isInfoEnabled()).thenReturn(true); String fooFileName = "./foo.js"; assertFileRemoved(fooFileName, frontendDirectory); try { updater.run(); Assert.fail("Execute should have failed with missing file"); } catch (IllegalStateException e) { assertThat(e.getMessage(), CoreMatchers .containsString(getFormattedFrontendErrorMessage( Sets.newSet(fooFileName)))); } }
@Test public void getModuleLines_oneFrontendDependencyAndFrontendDirectoryDontExist_throwExceptionAdvisingUserToRunPrepareFrontend() throws Exception { ClassFinder classFinder = getClassFinder(); updater = new UpdateImports(classFinder, getScanner(classFinder), tmpRoot, null); useMockLog = true; Mockito.when(logger.isInfoEnabled()).thenReturn(true); Files.move(frontendDirectory.toPath(), new File(tmpRoot, "_frontend").toPath()); try { updater.run(); Assert.fail("Execute should have failed with advice to run `prepare-frontend`"); } catch (IllegalStateException e) { assertThat(e.getMessage(), CoreMatchers.containsString( "Unable to locate frontend resources and missing token file. " + "Please run the `prepare-frontend` Vaadin plugin goal before deploying the application")); } }
@Test public void getModuleLines_multipleFrontendDependencyDoesntExist_throwExceptionAndlogExplanation() { useMockLog = true; Mockito.when(logger.isInfoEnabled()).thenReturn(true); String localTemplateFileName = "./local-template.js"; String fooFileName = "./foo.js"; assertFileRemoved(localTemplateFileName, frontendDirectory); assertFileRemoved(fooFileName, frontendDirectory); try { updater.run(); Assert.fail("Execute should have failed with missing files"); } catch (IllegalStateException e) { assertThat(e.getMessage(), CoreMatchers .containsString(getFormattedFrontendErrorMessage( Sets.newSet(localTemplateFileName, fooFileName)))); } }
@Test public void generateLines_resultingLinesContainsThemeLinesAndExpectedImportsAndCssLinesAndGeneratedImportsAndLoggerReports() throws Exception { List<String> expectedLines = new ArrayList<>( Arrays.asList("theme-line-foo", "theme-line-bar")); getExpectedImports().stream().filter(imp -> imp.equals("/foo.css")) .forEach(imp -> expectedLines .add("import '" + addWebpackPrefix(imp) + "';")); expectedLines.add( "import '@vaadin/vaadin-mixed-component/theme/lumo/vaadin-something-else';"); expectedLines.add("import 'unresolved/component';"); expectedLines.add( "import $css_0 from '@vaadin/vaadin-mixed-component/bar.css';"); expectedLines.add("import $css_1 from 'Frontend/foo.css';"); expectedLines.add("import $css_2 from 'Frontend/foo.css';"); expectedLines.add("import $css_3 from 'Frontend/foo.css';"); expectedLines.add("import $css_4 from 'Frontend/foo.css';"); expectedLines.add("import $css_5 from 'Frontend/foo.css';"); expectedLines.add("import $css_6 from 'Frontend/foo.css';"); expectedLines.add( "addCssBlock(`<custom-style><style>${$css_0}</style></custom-style>`);"); expectedLines.add( "addCssBlock(`<custom-style><style>${$css_1}</style></custom-style>`);"); expectedLines.add( "addCssBlock(`<custom-style><style include=\"bar\">${$css_2}</style></custom-style>`);"); expectedLines.add( "addCssBlock(`<dom-module id=\"baz\"><template><style>${$css_3}</style></template></dom-module>`);"); expectedLines.add( "addCssBlock(`<dom-module id=\"baz\"><template><style include=\"bar\">${$css_4}</style></template></dom-module>`);"); expectedLines.add( "addCssBlock(`<dom-module id=\"flow_css_mod_5\" theme-for=\"foo-bar\"><template><style>${$css_5}</style></template></dom-module>`);"); expectedLines.add( "addCssBlock(`<dom-module id=\"flow_css_mod_6\" theme-for=\"foo-bar\"><template><style include=\"bar\">${$css_6}</style></template></dom-module>`);"); expectedLines.add("import 'generated-modules-foo';"); expectedLines.add("import 'generated-modules-bar';"); updater.run(); for (String line : expectedLines) { Assert.assertTrue( "\n" + line + " IS NOT FOUND IN: \n" + updater.resultingLines, updater.resultingLines.contains(line)); } assertTrue(loggerFile.exists()); String output = FileUtils .readFileToString(loggerFile, StandardCharsets.UTF_8) .replace("\r", ""); Assert.assertThat(output, CoreMatchers.containsString( "Use the './' prefix for files in JAR files: 'ExampleConnector.js'")); Assert.assertThat(output, CoreMatchers .containsString("Use the './' prefix for files in the '" + frontendDirectory.getPath() + "' folder: 'vaadin-mixed-component/theme/lumo/vaadin-mixed-component.js'")); Assert.assertThat(output, CoreMatchers.containsString( "Failed to find the following imports in the `node_modules` tree:\n - unresolved/component")); Assert.assertThat(output, CoreMatchers.not(CoreMatchers.containsString( "changing 'frontend: }
@Test public void cssFileNotFound_throws() { assertTrue(resolveImportFile(frontendDirectory, nodeModulesPath, "@vaadin/vaadin-mixed-component/bar.css").delete()); exception.expect(IllegalStateException.class); updater.run(); }
@Test public void generate_containsLumoThemeFiles() { updater.run(); assertContainsImports(true, "@vaadin/vaadin-lumo-styles/color.js", "@vaadin/vaadin-lumo-styles/typography.js", "@vaadin/vaadin-lumo-styles/sizing.js", "@vaadin/vaadin-lumo-styles/spacing.js", "@vaadin/vaadin-lumo-styles/style.js", "@vaadin/vaadin-lumo-styles/icons.js"); }
@Test public void jsModuleOnRouterLayout_shouldBe_addedAfterLumoStyles() { updater.run(); assertContainsImports(true, "Frontend/common-js-file.js"); assertImportOrder("@vaadin/vaadin-lumo-styles/color.js", "Frontend/common-js-file.js"); assertImportOrder( "@vaadin/vaadin-mixed-component/theme/lumo/vaadin-something-else.js", "Frontend/common-js-file.js"); }
@Test public void jsModulesOrderIsPreservedAnsAfterJsModules() { updater.run(); assertImportOrder("jsmodule/g.js", "javascript/a.js", "javascript/b.js", "javascript/c.js"); }