From faeb319c58f0dbd592d7801a9a7f2d2ae3fa3818 Mon Sep 17 00:00:00 2001 From: Mikunj Date: Tue, 15 Jan 2019 12:21:15 +1100 Subject: [PATCH] Added profile model test. --- js/models/profile.js | 4 + test/index.html | 1 + test/models/conversations_test.js | 2 +- test/models/profile_test.js | 134 ++++++++++++++++++++++++++++++ 4 files changed, 140 insertions(+), 1 deletion(-) create mode 100644 test/models/profile_test.js diff --git a/js/models/profile.js b/js/models/profile.js index cd8b6ec9b..c20d49bbb 100644 --- a/js/models/profile.js +++ b/js/models/profile.js @@ -17,6 +17,10 @@ } 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(); diff --git a/test/index.html b/test/index.html index 192c6f556..55a02a55d 100644 --- a/test/index.html +++ b/test/index.html @@ -426,6 +426,7 @@ + diff --git a/test/models/conversations_test.js b/test/models/conversations_test.js index 62190fec7..de8798a97 100644 --- a/test/models/conversations_test.js +++ b/test/models/conversations_test.js @@ -192,4 +192,4 @@ describe('Conversation', () => { assert.isUndefined(collection.get(convo.id), 'got result for "+"'); }); }); -})(); +}); diff --git a/test/models/profile_test.js b/test/models/profile_test.js new file mode 100644 index 000000000..c57579272 --- /dev/null +++ b/test/models/profile_test.js @@ -0,0 +1,134 @@ +/* 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 expected = { + displayName: 'hi there!', + }; + const profile = storage.getLocalProfile(); + assert.exists(profile.name); + assert.deepEqual(expected, profile.name); + }); + + 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, + name: { + displayName: 'hi there!', + }, + }; + assert.deepEqual(expected, storage.getLocalProfile()); + }); + + it('trims the display name', async () => { + const values = [ + { + current: ' prefix', + expected: 'prefix', + }, + { + current: 'suffix ', + expected: 'suffix', + }, + { + current: 'in middle', + expected: 'in middle', + }, + ]; + + for(let i = 0; i < values.length; i += 1) { + const { current, expected } = values[i]; + await storage.setProfileName(current); + const profile = storage.getLocalProfile(); + const name = { + displayName: expected, + }; + assert.deepEqual(name, profile.name); + } + }); + + it('unsets the name property if it is empty', async () => { + const profile = { + name: { + displayName: 'HI THERE!', + }, + }; + await storage.put(PROFILE_ID, profile); + assert.exists(storage.getLocalProfile().name); + + await storage.setProfileName(''); + assert.notExists(storage.getLocalProfile().name); + }); + }); +}); \ No newline at end of file