diff --git a/spec/atom-environment-spec.coffee b/spec/atom-environment-spec.coffee index d1eabf2c8..17b9f51b0 100644 --- a/spec/atom-environment-spec.coffee +++ b/spec/atom-environment-spec.coffee @@ -321,14 +321,6 @@ describe "AtomEnvironment", -> expect(atom.workspace.open).not.toHaveBeenCalled() describe "adding a project folder", -> - it "adds a second path to the project", -> - initialPaths = atom.project.getPaths() - tempDirectory = temp.mkdirSync("a-new-directory") - spyOn(atom, "pickFolder").andCallFake (callback) -> - callback([tempDirectory]) - atom.addProjectFolder() - expect(atom.project.getPaths()).toEqual(initialPaths.concat([tempDirectory])) - it "does nothing if the user dismisses the file picker", -> initialPaths = atom.project.getPaths() tempDirectory = temp.mkdirSync("a-new-directory") @@ -336,6 +328,117 @@ describe "AtomEnvironment", -> atom.addProjectFolder() expect(atom.project.getPaths()).toEqual(initialPaths) + describe "when the project contains no folders", -> + describe "when there is saved state for the added folders", -> + projectPath = null + + beforeEach -> + atom.enablePersistence = true + [projectPath] = atom.project.getPaths() + waitsForPromise -> + Promise.all([ + atom.workspace.open(path.join(projectPath, 'script.js')) + atom.workspace.open(path.join(projectPath, 'sample.js')) + .then (e) -> e.insertText('changes') + ]) + + runs -> atom.workspace.getActivePane().splitRight() + waitsForPromise -> atom.workspace.open().then((e) -> e.setText('new editor')) + waitsForPromise -> atom.saveState() + runs -> atom.reset() + + afterEach -> + atom.enablePersistence = false + + it "restores the saved state", -> + spyOn(atom, "pickFolder").andCallFake (callback) -> + callback([projectPath]) + + waitsForPromise -> + atom.addProjectFolder() + + runs -> + expect(atom.project.getPaths()).toEqual([projectPath]) + expect(atom.workspace.getPanes().length).toEqual(2) + items = atom.workspace.getPaneItems() + expect(items.length).toEqual(3) + [unmodifiedNamedItem, modifiedNamedItem, modifiedUnnamedItem] = items + expect(unmodifiedNamedItem.getPath()).toEqual(path.join(projectPath, 'script.js')) + expect(unmodifiedNamedItem.isModified()).toBe(false) + expect(modifiedNamedItem.getPath()).toEqual(path.join(projectPath, 'sample.js')) + waitsFor -> modifiedNamedItem.isModified() + runs -> + expect(modifiedNamedItem.getText()).toMatch(/^changes/) + expect(modifiedUnnamedItem.getPath()).toEqual(undefined) + waitsFor -> modifiedUnnamedItem.isModified() + runs -> + expect(modifiedUnnamedItem.getText()).toEqual('new editor') + + it "maintains any existing dirty or named pane items", -> + # # TODO handle collisions + # waitsForPromise -> + # atom.workspace.open(path.join(projectPath, 'script.js')) + + waitsForPromise -> + Promise.all([ + atom.workspace.open(path.join(projectPath, 'css.css')) + atom.workspace.open(path.join(projectPath, 'lorem.txt')) + .then (e) -> e.insertText('changes') + atom.workspace.open().then (e) -> e.setText('another new editor') + atom.workspace.open() + ]) + + spyOn(atom, "pickFolder").andCallFake (callback) -> + callback([projectPath]) + + waitsForPromise -> + atom.addProjectFolder() + + runs -> + expect(atom.project.getPaths()).toEqual([projectPath]) + expect(atom.workspace.getPanes().length).toEqual(2) + items = atom.workspace.getPaneItems() + expect(items.length).toEqual(6) # 3 existing pane items, 3 from saved state + # discarded the empty, unnamed item (likely opened due to the "open empty editor on start" config option) + [modifiedUnnamedItem, unmodifiedNamedItem, modifiedNamedItem] = items + expect(unmodifiedNamedItem.getPath()).toEqual(path.join(projectPath, 'css.css')) + expect(unmodifiedNamedItem.isModified()).toBe(false) + expect(modifiedNamedItem.getPath()).toEqual(path.join(projectPath, 'lorem.txt')) + waitsFor -> modifiedNamedItem.isModified() + runs -> + expect(modifiedNamedItem.getText()).toMatch(/^changes/) + expect(modifiedUnnamedItem.getPath()).toEqual(undefined) + waitsFor -> modifiedUnnamedItem.isModified() + runs -> + expect(modifiedUnnamedItem.getText()).toEqual('another new editor') + + describe "when there is no saved state for the added folders", -> + beforeEach -> + spyOn(atom, 'loadState').andReturn(Promise.resolve(null)) + spyOn(atom, 'restoreStateIntoEnvironment') + + it "adds the selected folder to the project", -> + initialPaths = atom.project.setPaths([]) + tempDirectory = temp.mkdirSync("a-new-directory") + spyOn(atom, "pickFolder").andCallFake (callback) -> + callback([tempDirectory]) + waitsForPromise -> + atom.addProjectFolder() + runs -> + expect(atom.project.getPaths()).toEqual([tempDirectory]) + expect(atom.restoreStateIntoEnvironment).not.toHaveBeenCalled() + + describe "when the project already contains at least one folder", -> + it "adds a second path to the project", -> + initialPaths = atom.project.getPaths() + tempDirectory = temp.mkdirSync("a-new-directory") + spyOn(atom, "pickFolder").andCallFake (callback) -> + callback([tempDirectory]) + waitsForPromise -> + atom.addProjectFolder() + runs -> + expect(atom.project.getPaths()).toEqual(initialPaths.concat([tempDirectory])) + describe "::unloadEditorWindow()", -> it "saves the BlobStore so it can be loaded after reload", -> configDirPath = temp.mkdirSync('atom-spec-environment') diff --git a/src/atom-environment.coffee b/src/atom-environment.coffee index 51727fb30..0ae9779cd 100644 --- a/src/atom-environment.coffee +++ b/src/atom-environment.coffee @@ -861,7 +861,11 @@ class AtomEnvironment extends Model addProjectFolder: -> @pickFolder (selectedPaths = []) => - @project.addPath(selectedPath) for selectedPath in selectedPaths + @loadState(@getStateKey(selectedPaths)).then (state) => + if state && @project.getPaths().length is 0 + @restoreStateIntoEnvironment(state) + else + @project.addPath(selectedPath) for selectedPath in selectedPaths restoreStateIntoEnvironment: (state) -> shouldSerializeItem = (item) ->