Skip to content

Commit

Permalink
add navigation guard for backup loading
Browse files Browse the repository at this point in the history
-
Ticket: SUITEDEV-29114
  • Loading branch information
hawser86 committed Oct 29, 2021
1 parent 983365c commit 83b1ef1
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 5 deletions.
4 changes: 3 additions & 1 deletion src/renderer/components/app/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,9 @@ export default {
localStorage[LOCALSTORAGE_KEY_LAST_SELECTED_NAME] = this.secretName;
await this.loadSecret();
},
loadSelectedBackup(backup) {
async loadSelectedBackup(backup) {
if (this.hasSecretChanged && !(await notificationDisplayer.shouldChangesBeDiscarded())) return;

this.secret = objectToKeyValueArray(backup.data);
this.selectedBackupTime = backup.backupTime;
notificationDisplayer.backupSuccess();
Expand Down
49 changes: 45 additions & 4 deletions src/renderer/components/app/app.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -455,19 +455,20 @@ describe('App', () => {
sinon.stub(kubernetesClient, 'listContexts').resolves([]);
sinon.stub(kubernetesClient, 'listNamespaces').resolves([]);
const { vm } = await loadApp();
vm.secret = [{ key: 'FIELD1', value: 'value1' }, { key: 'FIELD2', value: 'value2' }];
vm.originalSecret = { FIELD1: 'val1', FIELD2: 'val2' };
vm.secret = [{ key: 'FIELD1', value: 'val1' }, { key: 'FIELD2', value: 'val2' }];

vm.loadSelectedBackup({ data: { FIELD1: 'value0', FIELD3: 'value3' }, backupTime: '2020-09-19T22:17:01.891Z' });
await vm.loadSelectedBackup({ data: { FIELD1: 'val0', FIELD3: 'val3' }, backupTime: '2020-09-19T22:17:01.891Z' });

expect(vm.secret).to.eql([{ key: 'FIELD1', value: 'value0' }, { key: 'FIELD3', value: 'value3' }]);
expect(vm.secret).to.eql([{ key: 'FIELD1', value: 'val0' }, { key: 'FIELD3', value: 'val3' }]);
});

it('should update selected backup time', async () => {
sinon.stub(kubernetesClient, 'listContexts').resolves([]);
sinon.stub(kubernetesClient, 'listNamespaces').resolves([]);
const { vm } = await loadApp();

vm.loadSelectedBackup({ data: { FIELD1: 'value0', FIELD3: 'value3' }, backupTime: '2020-09-19T22:17:01.891Z' });
await vm.loadSelectedBackup({ data: { FIELD1: 'val0', FIELD3: 'val3' }, backupTime: '2020-09-19T22:17:01.891Z' });

expect(vm.selectedBackupTime).to.eql('2020-09-19T22:17:01.891Z');
});
Expand All @@ -482,6 +483,46 @@ describe('App', () => {

expect(notificationDisplayer.backupSuccess).to.have.been.called;
});

it('should not replace secret when user cancels a change', async () => {
sinon.stub(kubernetesClient, 'listContexts').resolves([]);
sinon.stub(kubernetesClient, 'listNamespaces').resolves([]);
sinon.stub(notificationDisplayer, 'shouldChangesBeDiscarded').resolves(false);
const { vm } = await loadApp();
vm.originalSecret = { FIELD: 'original-value' };
vm.secret = [{ key: 'FIELD', value: 'modified-value' }];

await vm.loadSelectedBackup({ data: { FIELD: 'backup-value' }, backupTime: '2020-09-19T22:17:01.891Z' });

expect(vm.secret).to.eql([{ key: 'FIELD', value: 'modified-value' }]);
});

it('should replace secret when user discards unsaved changes', async () => {
sinon.stub(kubernetesClient, 'listContexts').resolves([]);
sinon.stub(kubernetesClient, 'listNamespaces').resolves([]);
sinon.stub(notificationDisplayer, 'shouldChangesBeDiscarded').resolves(true);
const { vm } = await loadApp();
vm.originalSecret = { FIELD: 'original-value' };
vm.secret = [{ key: 'FIELD', value: 'modified-value' }];

await vm.loadSelectedBackup({ data: { FIELD: 'backup-value' }, backupTime: '2020-09-19T22:17:01.891Z' });

expect(vm.secret).to.eql([{ key: 'FIELD', value: 'backup-value' }]);
});

it('should replace secret without confirmation when there are no changes in the secret', async () => {
sinon.stub(kubernetesClient, 'listContexts').resolves([]);
sinon.stub(kubernetesClient, 'listNamespaces').resolves([]);
sinon.stub(notificationDisplayer, 'shouldChangesBeDiscarded');
const { vm } = await loadApp();
vm.originalSecret = { FIELD: 'original-value' };
vm.secret = [{ key: 'FIELD', value: 'original-value' }];

await vm.loadSelectedBackup({ data: { FIELD: 'backup-value' }, backupTime: '2020-09-19T22:17:01.891Z' });

expect(vm.secret).to.eql([{ key: 'FIELD', value: 'backup-value' }]);
expect(notificationDisplayer.shouldChangesBeDiscarded).to.not.have.been.called;
});
});

describe('#reloadSecret', () => {
Expand Down

0 comments on commit 83b1ef1

Please sign in to comment.