commit
a6abcaf826
@ -1,56 +0,0 @@
|
||||
/* global storage, _ */
|
||||
/* global storage: false */
|
||||
|
||||
/* eslint-disable more/no-then */
|
||||
|
||||
// eslint-disable-next-line func-names
|
||||
(function() {
|
||||
'use strict';
|
||||
|
||||
window.Whisper = window.Whisper || {};
|
||||
|
||||
const PROFILE_ID = 'local-profile';
|
||||
|
||||
storage.getLocalProfile = () => {
|
||||
const profile = storage.get(PROFILE_ID, null);
|
||||
return profile;
|
||||
};
|
||||
|
||||
storage.setProfileName = async newName => {
|
||||
if (typeof newName !== 'string' && newName !== null) {
|
||||
throw Error('Name must be a string!');
|
||||
}
|
||||
|
||||
// Update our profiles accordingly'
|
||||
const trimmed = newName && newName.trim();
|
||||
|
||||
// If we get an empty name then unset the name property
|
||||
// Otherwise update it
|
||||
const profile = storage.getLocalProfile();
|
||||
const newProfile = { ...profile };
|
||||
if (_.isEmpty(trimmed)) {
|
||||
delete newProfile.displayName;
|
||||
} else {
|
||||
newProfile.displayName = trimmed;
|
||||
}
|
||||
|
||||
await storage.saveLocalProfile(newProfile);
|
||||
};
|
||||
|
||||
storage.saveLocalProfile = async profile => {
|
||||
const storedProfile = storage.get(PROFILE_ID, null);
|
||||
|
||||
// Only store the profile if we have a different object
|
||||
if (storedProfile && _.isEqual(storedProfile, profile)) {
|
||||
return;
|
||||
}
|
||||
|
||||
window.log.info('saving local profile ', profile);
|
||||
await storage.put(PROFILE_ID, profile);
|
||||
};
|
||||
|
||||
storage.removeLocalProfile = async () => {
|
||||
window.log.info('removing local profile');
|
||||
await storage.remove(PROFILE_ID);
|
||||
};
|
||||
})();
|
@ -1,108 +0,0 @@
|
||||
/* global storage */
|
||||
|
||||
/* eslint no-await-in-loop: 0 */
|
||||
|
||||
'use strict';
|
||||
|
||||
const PROFILE_ID = 'local-profile';
|
||||
|
||||
describe('Profile', () => {
|
||||
beforeEach(async () => {
|
||||
await clearDatabase();
|
||||
await storage.remove(PROFILE_ID);
|
||||
});
|
||||
|
||||
describe('getLocalProfile', () => {
|
||||
it('returns the local profile', async () => {
|
||||
const values = [null, 'hello', { a: 'b' }];
|
||||
for (let i = 0; i < values.length; i += 1) {
|
||||
await storage.put(PROFILE_ID, values[i]);
|
||||
assert.strictEqual(values[i], storage.getLocalProfile());
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
describe('saveLocalProfile', () => {
|
||||
it('saves a profile', async () => {
|
||||
const values = [null, 'hello', { a: 'b' }];
|
||||
for (let i = 0; i < values.length; i += 1) {
|
||||
await storage.saveLocalProfile(values[i]);
|
||||
assert.strictEqual(values[i], storage.get(PROFILE_ID));
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
describe('removeLocalProfile', () => {
|
||||
it('removes a profile', async () => {
|
||||
await storage.saveLocalProfile('a');
|
||||
assert.strictEqual('a', storage.getLocalProfile());
|
||||
|
||||
await storage.removeLocalProfile();
|
||||
assert.strictEqual(null, storage.getLocalProfile());
|
||||
});
|
||||
});
|
||||
|
||||
describe('setProfileName', () => {
|
||||
it('throws if a name is not a string', async () => {
|
||||
const values = [0, { a: 'b' }, [1, 2]];
|
||||
for (let i = 0; i < values.length; i += 1) {
|
||||
try {
|
||||
await storage.setProfileName(values[i]);
|
||||
assert.fail(
|
||||
`setProfileName did not throw an error for ${typeof values[i]}`
|
||||
);
|
||||
} catch (e) {
|
||||
assert.throws(() => {
|
||||
throw e;
|
||||
}, 'Name must be a string!');
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
it('does not throw if we pass a string or null', async () => {
|
||||
const values = [null, '1'];
|
||||
for (let i = 0; i < values.length; i += 1) {
|
||||
try {
|
||||
await storage.setProfileName(values[i]);
|
||||
} catch (e) {
|
||||
assert.fail('setProfileName threw an error');
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
it('saves the display name', async () => {
|
||||
await storage.setProfileName('hi there!');
|
||||
const profile = storage.getLocalProfile();
|
||||
assert.deepEqual(profile.displayName, 'hi there!');
|
||||
});
|
||||
|
||||
it('saves the display name without overwriting the other profile properties', async () => {
|
||||
const profile = { title: 'hello' };
|
||||
await storage.put(PROFILE_ID, profile);
|
||||
await storage.setProfileName('hi there!');
|
||||
|
||||
const expected = {
|
||||
...profile,
|
||||
displayName: 'hi there!',
|
||||
};
|
||||
assert.deepEqual(expected, storage.getLocalProfile());
|
||||
});
|
||||
|
||||
it('trims the display name', async () => {
|
||||
await storage.setProfileName(' in middle ');
|
||||
const profile = storage.getLocalProfile();
|
||||
assert.deepEqual('in middle', profile.displayName);
|
||||
});
|
||||
|
||||
it('unsets the display name property if it is empty', async () => {
|
||||
const profile = {
|
||||
displayName: 'HI THERE!',
|
||||
};
|
||||
await storage.put(PROFILE_ID, profile);
|
||||
assert.exists(storage.getLocalProfile().displayName);
|
||||
|
||||
await storage.setProfileName('');
|
||||
assert.notExists(storage.getLocalProfile().displayName);
|
||||
});
|
||||
});
|
||||
});
|
Loading…
Reference in New Issue