signal-desktop/test/modules/startup_test.js
2018-04-30 16:53:34 -04:00

122 lines
3.2 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

const sinon = require('sinon');
const { assert } = require('chai');
const Startup = require('../../js/modules/startup');
describe('Startup', () => {
const sandbox = sinon.createSandbox();
describe('syncReadReceiptConfiguration', () => {
afterEach(() => {
sandbox.restore();
});
it('should complete if user hasnt previously synced', async () => {
const deviceId = '2';
const sendRequestConfigurationSyncMessage = sandbox.spy();
const storagePutSpy = sandbox.spy();
const storage = {
get(name) {
if (name !== 'read-receipt-configuration-sync') {
return true;
}
return false;
},
put: storagePutSpy,
};
const expected = {
status: 'complete',
};
const actual = await Startup.syncReadReceiptConfiguration({
deviceId,
sendRequestConfigurationSyncMessage,
storage,
});
assert.deepEqual(actual, expected);
assert.equal(sendRequestConfigurationSyncMessage.callCount, 1);
assert.equal(storagePutSpy.callCount, 1);
assert(storagePutSpy.calledWith('read-receipt-configuration-sync', true));
});
it('should be skipped if this is the primary device', async () => {
const deviceId = '1';
const sendRequestConfigurationSyncMessage = () => {};
const storage = {};
const expected = {
status: 'skipped',
reason: 'isPrimaryDevice',
};
const actual = await Startup.syncReadReceiptConfiguration({
deviceId,
sendRequestConfigurationSyncMessage,
storage,
});
assert.deepEqual(actual, expected);
});
it('should be skipped if user has previously synced', async () => {
const deviceId = '2';
const sendRequestConfigurationSyncMessage = () => {};
const storage = {
get(name) {
if (name !== 'read-receipt-configuration-sync') {
return false;
}
return true;
},
};
const expected = {
status: 'skipped',
reason: 'hasPreviouslySynced',
};
const actual = await Startup.syncReadReceiptConfiguration({
deviceId,
sendRequestConfigurationSyncMessage,
storage,
});
assert.deepEqual(actual, expected);
});
it('should return error if sending of sync request fails', async () => {
const deviceId = '2';
const sendRequestConfigurationSyncMessage = sandbox.stub();
sendRequestConfigurationSyncMessage.rejects(new Error('boom'));
const storagePutSpy = sandbox.spy();
const storage = {
get(name) {
if (name !== 'read-receipt-configuration-sync') {
return true;
}
return false;
},
put: storagePutSpy,
};
const actual = await Startup.syncReadReceiptConfiguration({
deviceId,
sendRequestConfigurationSyncMessage,
storage,
});
assert.equal(actual.status, 'error');
assert.include(actual.error, 'boom');
assert.equal(sendRequestConfigurationSyncMessage.callCount, 1);
assert.equal(storagePutSpy.callCount, 0);
});
});
});