From 49af35f00448f4f74c17ce70ded7171fa2be41da Mon Sep 17 00:00:00 2001 From: Tim Glasgow Date: Sat, 30 Jul 2022 00:10:16 -0500 Subject: [PATCH] system repo coverage --- squarenotsquare/__mock__/mockRealm.js | 135 +++++++++++++++++++++++++ squarenotsquare/__tests__/Repo-test.js | 44 ++++++++ 2 files changed, 179 insertions(+) create mode 100644 squarenotsquare/__mock__/mockRealm.js diff --git a/squarenotsquare/__mock__/mockRealm.js b/squarenotsquare/__mock__/mockRealm.js new file mode 100644 index 0000000..37be736 --- /dev/null +++ b/squarenotsquare/__mock__/mockRealm.js @@ -0,0 +1,135 @@ +// https://github.com/realm/realm-js/issues/370#issuecomment-270849466 +export default class MockRealm { + constructor(params) { + this.schema = {}; + this.schemaVersion = params.schemaVersion; + this.callbackList = []; + this.data = {}; + this.schemaCallbackList = {}; + params.schema.forEach((schema) => { + this.data[schema.name] = {}; + }); + params.schema.forEach((schema) => { + this.schema[schema.name] = schema; + }); + this.lastLookedUpModel = null; + } + + objects(schemaName) { + this.lastLookedUpModel = schemaName; + const objects = Object.values(this.data[schemaName]); + objects.values = () => objects; + objects.sorted = () => this.compareFunc ? objects.sort(this.compareFunc) : objects.sort(); + objects.addListener = (cb) => { + if (this.schemaCallbackList[schemaName]) { + this.schemaCallbackList[schemaName].push(cb); + } else { + this.schemaCallbackList[schemaName] = [cb]; + } + }; + objects.removeListener = () => {}; + objects.filtered = this.filtered ? this.filtered.bind(this, schemaName) : () => objects; + return objects; + } + + write(fn) { + this.writing = true; + fn(); + this.writing = false; + } + + create(schemaName, object) { + const modelObject = object; + const properties = this.schema[schemaName].schema.properties; + Object.keys(properties).forEach((key) => { + if (modelObject[key] && modelObject[key].model) { + this.data[modelObject[key].model][modelObject[key].id] = this.create( + modelObject[key].model, modelObject[key], + ); + } else if (modelObject[key] && modelObject[key].length && modelObject[key][0].model) { + modelObject[key].forEach((obj) => { + this.data[modelObject[key][0].model][obj.id] = obj; + }); + modelObject[key].filtered = this.filtered ? this.filtered : () => modelObject[key]; + modelObject[key].sorted = () => modelObject[key].sort(); + } else if (modelObject[key] === undefined) { + if (typeof properties[key] === 'object' && properties[key].optional) { + modelObject[key] = null; + } + if (typeof properties[key] === 'object' && ['list', 'linkingObjects'].includes(properties[key].type)) { + modelObject[key] = []; + modelObject[key].filtered = () => []; + modelObject[key].sorted = () => []; + } + } + }); + + this.data[schemaName][modelObject.id] = modelObject; + if (this.writing) { + if (this.schemaCallbackList[schemaName]) { + this.schemaCallbackList[schemaName].forEach(cb => cb(schemaName, { + insertions: { length: 1 }, + modifications: { length: 0 }, + deletions: { length: 0 }, + })); + } + this.callbackList.forEach((cb) => { cb(); }); + } + return modelObject; + } + + objectForPrimaryKey(model, id) { + this.lastLookedUpModel = model; + return this.data[model][id]; + } + + delete(object) { + if (this.lastLookedUpModel || object.model) { + const model = object.model ? object.model : this.lastLookedUpModel + if (Array.isArray(object)) { + object.forEach((item) => { + delete this.data[model][item.id]; + }); + } + delete this.data[model][object.id]; + if (this.writing) { + if (this.schemaCallbackList[model]) { + this.schemaCallbackList[model].forEach(cb => cb(model, { + insertions: { length: 0 }, + modifications: { length: 0 }, + deletions: { length: 1 }, + })); + } + this.callbackList.forEach((cb) => { cb(); }); + } + } + } + + deleteAll() { + Object.keys(this.schema).forEach((key) => { + if (this.writing && this.schemaCallbackList[this.schema[key].name]) { + this.schemaCallbackList[this.schema[key].name].forEach(cb => cb(key, { + insertions: { length: 0 }, + modifications: { length: 0 }, + deletions: { length: Object.values(this.data[this.schema[key].name]).length }, + })); + } + this.data[this.schema[key].name] = {}; + }); + if (this.writing) this.callbackList.forEach((cb) => { cb(); }); + } + + addListener(event, callback) { + this.callbackList.push(callback); + } + + prepareData(schemaName, objects) { + objects.forEach((object) => { + this.create(schemaName, object); + }); + } + } + + MockRealm.Object = class Object { + isValid() { return true; } + }; \ No newline at end of file diff --git a/squarenotsquare/__tests__/Repo-test.js b/squarenotsquare/__tests__/Repo-test.js index c47109f..2df6b6f 100644 --- a/squarenotsquare/__tests__/Repo-test.js +++ b/squarenotsquare/__tests__/Repo-test.js @@ -59,6 +59,21 @@ test('SystemRepo creates system value', () => { expect(object.value).toEqual('isNew'); }) +test('SystemRepo handles creation of existing system value', () => { + let mockRealm = new MockRealm(0); + let testRepo = new SystemRepo(mockRealm); + + let newSystemKey = { + key: 'newKey', + value: 'isNew' + }; + + testRepo.createSystemValue(newSystemKey.key, newSystemKey.value); + testRepo.createSystemValue(newSystemKey.key, newSystemKey.value); + let object = mockRealm[System.name][0]; + expect(object.value).toEqual('isNew'); +}) + test('SystemRepo deletes existing system value', () => { let mockRealm = new MockRealm(0); let testRepo = new SystemRepo(mockRealm); @@ -72,4 +87,33 @@ test('SystemRepo deletes existing system value', () => { testRepo.deleteSystemValue(newSystemKey.key); expect(mockRealm.deletedKeys[0].value).toEqual(newSystemKey.value); +}) + +test('SystemRepo deletes non-existing system value', () => { + let mockRealm = new MockRealm(0); + let testRepo = new SystemRepo(mockRealm); + + testRepo.deleteSystemValue('key'); + expect(mockRealm.deletedKeys.length).toEqual(0); +}) + +test('SystemRepo gets multiple system values', () => { + let mockRealm = new MockRealm(0); + let testRepo = new SystemRepo(mockRealm); + + let systemKey1 = { + id: uuidv4(), + key: 'key1', + value: 'val1' + }; + + let systemKey2 = { + id: uuidv4(), + key: 'key1', + value: 'val1' + }; + + mockRealm.create(System.name, systemKey1); + mockRealm.create(System.name, systemKey2); + testRepo.getSystemKeyValue(systemKey1.key); }) \ No newline at end of file