Search in sources :

Example 6 with FrontendDependencies

use of com.vaadin.flow.server.frontend.scanner.FrontendDependencies in project flow by vaadin.

the class AbstractNodeUpdatePackagesTest method generatePackageJson_sameDependenciesInDifferentOrder_updaterIsNotModified.

@Test
public void generatePackageJson_sameDependenciesInDifferentOrder_updaterIsNotModified() throws IOException {
    FrontendDependencies frontendDependencies = Mockito.mock(FrontendDependencies.class);
    Map<String, String> packages = new HashMap<>();
    packages.put("@polymer/iron-list", "3.0.2");
    packages.put("@vaadin/vaadin-confirm-dialog", "1.1.4");
    packages.put("@vaadin/vaadin-checkbox", "2.2.10");
    packages.put("@polymer/iron-icon", "3.0.1");
    packages.put("@vaadin/vaadin-time-picker", "2.0.2");
    Mockito.when(frontendDependencies.getPackages()).thenReturn(packages);
    packageUpdater = new TaskUpdatePackages(classFinder, frontendDependencies, baseDir, generatedDir, resourcesDir, false, false, TARGET, featureFlags);
    packageCreator.execute();
    packageUpdater.execute();
    // Shuffle the dependencies.
    JsonObject json = getPackageJson(this.packageJson);
    JsonObject dependencies = json.getObject(DEPENDENCIES);
    List<String> dependencyKeys = Arrays.asList(dependencies.keys());
    Collections.shuffle(dependencyKeys);
    JsonObject newDependencies = Json.createObject();
    dependencyKeys.forEach(key -> newDependencies.put(key, dependencies.getString(key)));
    json.put(DEPENDENCIES, newDependencies);
    Files.write(this.packageJson.toPath(), Collections.singletonList(stringify(json)));
    // generate it one more time, the content will be different since
    // packageCreator has not added its content
    packageUpdater.execute();
    Assert.assertFalse("Modification flag should be false when no dependencies changed.", packageUpdater.modified);
}
Also used : HashMap(java.util.HashMap) JsonObject(elemental.json.JsonObject) FrontendDependencies(com.vaadin.flow.server.frontend.scanner.FrontendDependencies) Test(org.junit.Test)

Example 7 with FrontendDependencies

use of com.vaadin.flow.server.frontend.scanner.FrontendDependencies in project flow by vaadin.

the class AbstractNodeUpdatePackagesTest method updatedPackageJson_noDependencies_creatorAndUpdatedIsMarkedModified.

@Test
public void updatedPackageJson_noDependencies_creatorAndUpdatedIsMarkedModified() {
    FrontendDependencies frontendDependencies = Mockito.mock(FrontendDependencies.class);
    Map<String, String> packages = new HashMap<>();
    Mockito.when(frontendDependencies.getPackages()).thenReturn(packages);
    packageUpdater = new TaskUpdatePackages(classFinder, frontendDependencies, baseDir, generatedDir, resourcesDir, false, false, TARGET, featureFlags);
    packageCreator.execute();
    packageUpdater.execute();
    Assert.assertTrue("Modification flag should be true when main package was created.", packageCreator.modified);
    Assert.assertTrue("Modification flag should be true as we should have updated the hash for default dependencies.", packageUpdater.modified);
}
Also used : HashMap(java.util.HashMap) FrontendDependencies(com.vaadin.flow.server.frontend.scanner.FrontendDependencies) Test(org.junit.Test)

Example 8 with FrontendDependencies

use of com.vaadin.flow.server.frontend.scanner.FrontendDependencies in project flow by vaadin.

the class UpdateThemedImportsTest method setup.

@Before
public void setup() throws Exception {
    File tmpRoot = temporaryFolder.getRoot();
    frontendDirectory = new File(tmpRoot, DEFAULT_FRONTEND_DIR);
    nodeModulesPath = new File(tmpRoot, NODE_MODULES);
    generatedPath = new File(tmpRoot, Paths.get(TARGET, DEFAULT_GENERATED_DIR).toString());
    importsFile = new File(generatedPath, IMPORTS_NAME);
    Assert.assertTrue(nodeModulesPath.mkdirs());
    createImport("./src/subfolder/sub-template.js", "");
    createImport("./src/client-side-template.js", "import 'xx' from './subfolder/sub-template.js';" + "import '@vaadin/vaadin-button/src/vaadin-button.js'");
    createImport("./src/client-side-no-themed-template.js", "");
    createImport("./src/main-template.js", "import 'xx' from './client-side-template.js';" + "import \"./client-side-no-themed-template.js\";" + "import './src/wrong-themed-template.js';" + "import '@vaadin/vaadin-button/src/vaadin-button.js'");
    // create themed modules
    createImport("./theme/myTheme/subfolder/sub-template.js", "");
    createImport("./theme/myTheme/client-side-template.js", "");
    createImport("./theme/myTheme/main-template.js", "");
    // wrong-themed-template.js should not be resolved inside node_modules.
    // It should be searched only inside frontend directory
    createImport("theme/myTheme/wrong-themed-template.js", "");
    // create css files to avoid exception when files not found during the
    // test
    createImport("./foo.css", "");
    createImport("@vaadin/vaadin-mixed-component/bar.css", "");
    // make external component's module and its themed version
    createImport("@vaadin/vaadin-button/src/vaadin-button.js", "");
    createImport("@vaadin/vaadin-button/theme/myTheme/vaadin-button.js", "");
    ClassFinder finder = getClassFinder();
    FrontendDependencies deps = new FrontendDependencies(finder) {

        @Override
        public List<String> getModules() {
            return Stream.of("./src/main-template.js").collect(Collectors.toList());
        }

        @Override
        public Set<String> getScripts() {
            return Collections.emptySet();
        }

        @Override
        public AbstractTheme getTheme() {
            return new MyTheme();
        }

        @Override
        public ThemeDefinition getThemeDefinition() {
            return new ThemeDefinition(MyTheme.class, "", "");
        }
    };
    updater = new TaskUpdateImports(finder, deps, cf -> null, tmpRoot, generatedPath, frontendDirectory, null, null, false, TARGET, true, false, Mockito.mock(FeatureFlags.class));
}
Also used : CoreMatchers(org.hamcrest.CoreMatchers) DEFAULT_FRONTEND_DIR(com.vaadin.flow.server.frontend.FrontendUtils.DEFAULT_FRONTEND_DIR) DEFAULT_GENERATED_DIR(com.vaadin.flow.server.frontend.FrontendUtils.DEFAULT_GENERATED_DIR) StringUtils(org.apache.commons.lang3.StringUtils) FrontendDependencies(com.vaadin.flow.server.frontend.scanner.FrontendDependencies) Charset(java.nio.charset.Charset) ThemeDefinition(com.vaadin.flow.theme.ThemeDefinition) ExpectedException(org.junit.rules.ExpectedException) AbstractTheme(com.vaadin.flow.theme.AbstractTheme) Before(org.junit.Before) ClassFinder(com.vaadin.flow.server.frontend.scanner.ClassFinder) Files(java.nio.file.Files) NODE_MODULES(com.vaadin.flow.server.frontend.FrontendUtils.NODE_MODULES) Set(java.util.Set) IOException(java.io.IOException) FileUtils(org.apache.commons.io.FileUtils) Test(org.junit.Test) TARGET(com.vaadin.flow.server.Constants.TARGET) Collectors(java.util.stream.Collectors) File(java.io.File) IMPORTS_NAME(com.vaadin.flow.server.frontend.FrontendUtils.IMPORTS_NAME) Mockito(org.mockito.Mockito) List(java.util.List) Stream(java.util.stream.Stream) MatcherAssert(org.hamcrest.MatcherAssert) Rule(org.junit.Rule) Paths(java.nio.file.Paths) FeatureFlags(com.vaadin.experimental.FeatureFlags) Assert(org.junit.Assert) Collections(java.util.Collections) TemporaryFolder(org.junit.rules.TemporaryFolder) ClassFinder(com.vaadin.flow.server.frontend.scanner.ClassFinder) File(java.io.File) FrontendDependencies(com.vaadin.flow.server.frontend.scanner.FrontendDependencies) ThemeDefinition(com.vaadin.flow.theme.ThemeDefinition) Before(org.junit.Before)

Example 9 with FrontendDependencies

use of com.vaadin.flow.server.frontend.scanner.FrontendDependencies in project flow by vaadin.

the class AbstractNodeUpdatePackagesTest method removedFrameworkDependencies_dependencyIsRemoved.

@Test
public void removedFrameworkDependencies_dependencyIsRemoved() throws IOException {
    FrontendDependencies frontendDependencies = Mockito.mock(FrontendDependencies.class);
    Map<String, String> packages = new HashMap<>();
    packages.put("@polymer/iron-list", "3.0.2");
    packages.put("@vaadin/vaadin-confirm-dialog", "1.1.4");
    packages.put("@vaadin/vaadin-checkbox", "2.2.10");
    packages.put("@polymer/iron-icon", "3.0.1");
    packages.put("@vaadin/vaadin-time-picker", "2.0.2");
    Mockito.when(frontendDependencies.getPackages()).thenReturn(packages);
    packageUpdater = new TaskUpdatePackages(classFinder, frontendDependencies, baseDir, generatedDir, resourcesDir, false, false, TARGET, featureFlags);
    packageCreator.execute();
    packageUpdater.execute();
    packages.remove("@vaadin/vaadin-checkbox");
    JsonObject dependencies = getPackageJson(packageJson).getObject(DEPENDENCIES);
    Assert.assertTrue("vaadin-checkbox is missing from the dependencies", dependencies.hasKey("@vaadin/vaadin-checkbox"));
    dependencies = getPackageJson(packageJson).getObject(VAADIN_DEP_KEY).getObject(DEPENDENCIES);
    Assert.assertTrue("vaadin-checkbox is missing from vaadin.dependencies", dependencies.hasKey("@vaadin/vaadin-checkbox"));
    // generate it one more time, this should remove the checkbox
    packageUpdater.execute();
    dependencies = getPackageJson(packageJson).getObject(DEPENDENCIES);
    Assert.assertFalse("vaadin-checkbox is still available in the dependencies", dependencies.hasKey("@vaadin/vaadin-checkbox"));
    dependencies = getPackageJson(packageJson).getObject(VAADIN_DEP_KEY).getObject(DEPENDENCIES);
    Assert.assertFalse("vaadin-checkbox is still available in vaadin.dependencies", dependencies.hasKey("@vaadin/vaadin-checkbox"));
}
Also used : HashMap(java.util.HashMap) JsonObject(elemental.json.JsonObject) FrontendDependencies(com.vaadin.flow.server.frontend.scanner.FrontendDependencies) Test(org.junit.Test)

Example 10 with FrontendDependencies

use of com.vaadin.flow.server.frontend.scanner.FrontendDependencies in project flow by vaadin.

the class AbstractNodeUpdatePackagesTest method userAddedDependencies_notCleanedByUpdater.

@Test
public void userAddedDependencies_notCleanedByUpdater() throws IOException {
    FrontendDependencies frontendDependencies = Mockito.mock(FrontendDependencies.class);
    Map<String, String> packages = new HashMap<>();
    packages.put("@polymer/iron-list", "3.0.2");
    packages.put("@vaadin/vaadin-confirm-dialog", "1.1.4");
    packages.put("@vaadin/vaadin-checkbox", "2.2.10");
    packages.put("@polymer/iron-icon", "3.0.1");
    packages.put("@vaadin/vaadin-time-picker", "2.0.2");
    Mockito.when(frontendDependencies.getPackages()).thenReturn(packages);
    packageUpdater = new TaskUpdatePackages(classFinder, frontendDependencies, baseDir, generatedDir, resourcesDir, false, false, TARGET, featureFlags);
    packageCreator.execute();
    JsonObject json = getPackageJson(packageJson);
    json.getObject(DEPENDENCIES).put("@custom/timer", "3.3.0");
    Files.write(packageJson.toPath(), Collections.singletonList(json.toJson()));
    packageUpdater.execute();
    JsonObject dependencies = getPackageJson(packageJson).getObject(DEPENDENCIES);
    Assert.assertTrue(dependencies.hasKey("@custom/timer"));
    Assert.assertEquals("3.3.0", dependencies.getString("@custom/timer"));
}
Also used : HashMap(java.util.HashMap) JsonObject(elemental.json.JsonObject) FrontendDependencies(com.vaadin.flow.server.frontend.scanner.FrontendDependencies) Test(org.junit.Test)

Aggregations

FrontendDependencies (com.vaadin.flow.server.frontend.scanner.FrontendDependencies)12 Test (org.junit.Test)11 HashMap (java.util.HashMap)10 JsonObject (elemental.json.JsonObject)4 FeatureFlags (com.vaadin.experimental.FeatureFlags)2 TARGET (com.vaadin.flow.server.Constants.TARGET)1 DEFAULT_FRONTEND_DIR (com.vaadin.flow.server.frontend.FrontendUtils.DEFAULT_FRONTEND_DIR)1 DEFAULT_GENERATED_DIR (com.vaadin.flow.server.frontend.FrontendUtils.DEFAULT_GENERATED_DIR)1 IMPORTS_NAME (com.vaadin.flow.server.frontend.FrontendUtils.IMPORTS_NAME)1 NODE_MODULES (com.vaadin.flow.server.frontend.FrontendUtils.NODE_MODULES)1 ClassFinder (com.vaadin.flow.server.frontend.scanner.ClassFinder)1 AbstractTheme (com.vaadin.flow.theme.AbstractTheme)1 ThemeDefinition (com.vaadin.flow.theme.ThemeDefinition)1 File (java.io.File)1 IOException (java.io.IOException)1 Charset (java.nio.charset.Charset)1 Files (java.nio.file.Files)1 Paths (java.nio.file.Paths)1 Collections (java.util.Collections)1 List (java.util.List)1