Skip to content

Commit

Permalink
fix: only save if needed when closing
Browse files Browse the repository at this point in the history
Including tests that open and close do not save unedited files.
Test is currently still failing for open.

See #3543.

Signed-off-by: Max <max@nextcloud.com>
  • Loading branch information
max-nextcloud committed Jan 5, 2023
1 parent 908c1ac commit 2ac410e
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 18 deletions.
62 changes: 62 additions & 0 deletions cypress/e2e/sync.spec.js
@@ -0,0 +1,62 @@
/**
* @copyright Copyright (c) 2023 Max <max@nextcloud.com>
*
* @author Max <max@nextcloud.com>
*
* @license AGPL-3.0-or-later
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/

import { randUser } from '../utils/index.js'

const user = randUser()

describe('Text and server mimetypes', () => {
before(() => {
cy.createUser(user)
})

beforeEach(() => {
cy.login(user)
cy.uploadTestFile('test.md')
cy.visit('/apps/files')
})

// still failing due to headings creating transactions
it.skip('does not save file when opening', () => {
cy.intercept({ method: 'POST', url: '**/apps/text/session/sync' })
.as('sync')
cy.openTestFile()
cy.wait('@sync', { timeout: 7000 }).its('request.body').should('not.have.property', 'autosaveContent')
cy.wait('@sync', { timeout: 7000 }).its('request.body').should('not.have.property', 'autosaveContent')
cy.wait('@sync', { timeout: 7000 }).its('request.body').should('not.have.property', 'autosaveContent')
cy.wait('@sync', { timeout: 7000 }).its('request.body').should('not.have.property', 'autosaveContent')
cy.getContent().find('h2').should('contain', 'Hello world')
})

it('does not save file when closing', () => {
cy.intercept({ method: 'POST', url: '**/apps/text/session/sync' })
.as('sync')
cy.openTestFile()
cy.getContent().find('h2').should('contain', 'Hello world')
// Allow the sync to stabelize so no steps are pending.
cy.wait('@sync', { timeout: 7000 })
cy.wait('@sync', { timeout: 7000 })
cy.wait('@sync', { timeout: 7000 })
cy.closeFile()
cy.wait('@sync').its('request.body').should('not.have.property', 'autosaveContent')
})
})
8 changes: 8 additions & 0 deletions cypress/support/commands.js
Expand Up @@ -133,6 +133,14 @@ Cypress.Commands.add('visitTestFolder', (visitOptions = {}) => {
})
})

Cypress.Commands.add('uploadTestFile', source => {
cy.testName().then(name => cy.uploadFile(source, 'text/markdown', `${name}.md`))
})

Cypress.Commands.add('openTestFile', () => {
cy.testName().then(name => cy.openFile(`${name}.md`))
})

Cypress.Commands.add('isolateTest', ({ sourceFile = 'empty.md', targetFile = null, onBeforeLoad } = {}) => {
targetFile = targetFile || sourceFile
cy.createTestFolder().then(folderName => {
Expand Down
3 changes: 2 additions & 1 deletion src/services/PollingBackend.js
Expand Up @@ -119,7 +119,7 @@ class PollingBackend {
) {
autosaveContent = this._authority._getContent()
}
axios.post(endpointUrl('session/sync', this._isPublic()), {
const request = axios.post(endpointUrl('session/sync', this._isPublic()), {
documentId: this._authority.document.id,
sessionId: this._authority.session.id,
sessionToken: this._authority.session.token,
Expand All @@ -132,6 +132,7 @@ class PollingBackend {
}).then(this._handleResponse.bind(this), this._handleError.bind(this))
this._manualSave = false
this._forcedSave = false
return request
}

_handleResponse(response) {
Expand Down
24 changes: 7 additions & 17 deletions src/services/SyncService.js
Expand Up @@ -234,24 +234,14 @@ class SyncService {
}
}

saveIfNeeded() {
return this.backend.fetchSteps()
}

close() {
let closed = false
return new Promise((resolve, reject) => {
this.on('save', () => {
this._close().then(() => {
closed = true
resolve()
}).catch(() => resolve())
})
setTimeout(() => {
if (!closed) {
this._close().then(() => {
resolve()
}).catch(() => resolve())
}
}, 2000)
this.save()
})
const timeout = new Promise((resolve) => setTimeout(resolve, 2000))
return Promise.any([timeout, this.saveIfNeeded()])
.then(() => this._close()).catch(console.info)
}

_close() {
Expand Down

0 comments on commit 2ac410e

Please sign in to comment.