{"size": 6241, "ext": "js", "max_stars_repo_path": "e2e/tests/appManager/API/inMemory/clear.spec.js", "max_stars_repo_name": "kenyuen/core", "max_stars_repo_stars_event_min_datetime": "2020-04-02T14:37:34.000Z", "max_stars_repo_stars_event_max_datetime": "2022-03-28T14:43:53.000Z", "max_issues_repo_path": "e2e/tests/appManager/API/inMemory/clear.spec.js", "max_issues_repo_name": "kenyuen/core", "max_issues_repo_issues_event_min_datetime": "2020-04-02T15:39:30.000Z", "max_issues_repo_issues_event_max_datetime": "2022-03-22T07:48:34.000Z", "max_forks_repo_path": "e2e/tests/appManager/API/inMemory/clear.spec.js", "max_forks_repo_name": "kenyuen/core", "max_forks_repo_forks_event_min_datetime": "2020-04-02T21:01:40.000Z", "max_forks_repo_forks_event_max_datetime": "2022-02-10T19:06:53.000Z"}
{"max_stars_count": 81, "max_issues_count": 103, "max_forks_count": 28, "avg_line_length": 30.2961165049, "max_line_length": 113, "alphanum_fraction": 0.5444640282}
describe('clear() ', function () {
    this.timeout(60000);
    let definitionsOnStart;
    let unsubs = [];
    let timeout;

    const baseDefinition = {
        name: "SimpleOne",
        type: "window",
        title: "SimpleOne",
        details: {
            url: "http://localhost:4242/dummyApp/index.html"
        },
        customProperties: {
            includeInWorkspaces: true
        }
    };

    const getMassApps = (numberOfDefs, namePrefix) => {
        const originalName = baseDefinition.name;

        const apps = Array.from({ length: numberOfDefs }).map((el, idx) => {

            const name = namePrefix ? namePrefix + originalName + idx.toString() : originalName + idx.toString();

            return Object.assign({}, baseDefinition, { name });
        });

        return apps;
    };

    const extraDefOne = {
        name: "ExtraOne",
        type: "window",
        details: {
            url: "http://localhost:4242/dummyApp/index.html"
        },
        customProperties: {
            includeInWorkspaces: true
        }
    };

    const extraDefTwo = {
        name: "ExtraTwo",
        type: "window",
        details: {
            url: "http://localhost:4242/dummyApp/index.html"
        },
        customProperties: {
            includeInWorkspaces: true
        }
    };


    before(async () => {
        await coreReady;

        definitionsOnStart = await glue.appManager.inMemory.export();
        await glue.appManager.inMemory.import([]);
    });

    afterEach(async () => {
        if (timeout) {
            clearTimeout(timeout);
            timeout = undefined;
        }

        await glue.appManager.inMemory.import([]);

        if (unsubs.length) {
            unsubs.forEach((un) => un());
            unsubs = [];
        }
    });

    after(() => glue.appManager.inMemory.import(definitionsOnStart, "replace"));

    it('should return a promise', async () => {
        const clearPromise = glue.appManager.inMemory.clear();

        expect(clearPromise.then).to.be.a("function");
        expect(clearPromise.catch).to.be.a("function");

        await clearPromise;
    });

    it('should resolve and do not change state if no definitions initially', async () => {
        const beforeDefs = await glue.appManager.inMemory.export();
        const beforeApps = glue.appManager.applications();

        await glue.appManager.inMemory.clear();

        const afterDefs = await glue.appManager.inMemory.export();
        const afterApps = glue.appManager.applications();

        expect(beforeApps).to.eql(afterApps);
        expect(beforeDefs).to.eql(afterDefs);
    });

    it('should resolve and not notify about removed definitions if no definitions initially', (done) => {
        const ready = gtf.waitFor(2, done);

        timeout = setTimeout(ready, 3000);

        const unsub = glue.appManager.onAppRemoved((app) => {
            done(`Received unexpected app removed event for: ${app.name}`);
        });

        unsubs.push(unsub);

        glue.appManager.inMemory.clear().then(ready).catch(done);
    });

    it('should remove all definitions if one definition present', async () => {
        await glue.appManager.inMemory.import([extraDefOne]);
        await glue.appManager.inMemory.clear();

        const afterDefs = await glue.appManager.inMemory.export();
        const afterApps = glue.appManager.applications();

        expect(afterDefs).to.eql([]);
        expect(afterApps).to.eql([]);
    });

    it('should notify of a correct removed definition if one definitions was present', (done) => {
        const ready = gtf.waitFor(2, done);

        glue.appManager.inMemory.import([extraDefOne])
            .then(() => {
                const unsub = glue.appManager.onAppRemoved((app) => {
                    try {
                        expect(app.name).to.eql(extraDefOne.name);
                        ready();
                    } catch (error) {
                        done(error);
                    }
                });

                unsubs.push(unsub);

                return glue.appManager.inMemory.clear();
            })
            .then(ready).catch(done);

    });

    it('should remove all definition if two definitions were present', async () => {
        await glue.appManager.inMemory.import([extraDefOne, extraDefTwo]);
        await glue.appManager.inMemory.clear();

        const afterDefs = await glue.appManager.inMemory.export();
        const afterApps = glue.appManager.applications();

        expect(afterDefs).to.eql([]);
        expect(afterApps).to.eql([]);
    });

    it('should notify of two correct removed definition if two definitions were present', (done) => {
        const ready = gtf.waitFor(3, done);
        const expectedNames = [extraDefOne.name, extraDefTwo.name];

        glue.appManager.inMemory.import([extraDefOne, extraDefTwo])
            .then(() => {
                const unsub = glue.appManager.onAppRemoved((app) => {
                    try {
                        
                        if (!expectedNames.length) {
                            done("No more definitions are expected as removed");
                            return;
                        }

                        const foundAppIndex = expectedNames.findIndex((n) => n === app.name);

                        expect(foundAppIndex >= 0).to.be.true;

                        expectedNames.splice(foundAppIndex, 1);
                        ready();
                    } catch (error) {
                        done(error);
                    }
                });

                unsubs.push(unsub);

                return glue.appManager.inMemory.clear();
            })
            .then(ready).catch(done);
    });

    it('should wait a running bulk import and correctly remove all definitions', async () => {
        const massDefs = getMassApps(9000);

        await Promise.all([
            glue.appManager.inMemory.import(massDefs, "replace"),
            glue.appManager.inMemory.clear()
        ]);

        expect(glue.appManager.applications().length).to.eql(0);

        const exported = await glue.appManager.inMemory.export();

        expect(exported.length).to.eql(0);
    });

});
