From 0802420bf4c836f19326fce8470b213d097c1f0f Mon Sep 17 00:00:00 2001 From: Binnette Date: Tue, 23 Jul 2024 00:27:04 +0200 Subject: [PATCH] Add tests for preferences --- src/__mocks__/requests.js | 27 ++++++++++++++ .../__snapshots__/index.test.js.snap | 24 ++++++++++++ src/__tests__/index.test.js | 37 +++++++++++++++++++ 3 files changed, 88 insertions(+) diff --git a/src/__mocks__/requests.js b/src/__mocks__/requests.js index fb8fbd6..ddff246 100644 --- a/src/__mocks__/requests.js +++ b/src/__mocks__/requests.js @@ -432,6 +432,33 @@ export const fetchUserRequest = jest.fn().mockImplementation(() => ({ } })); +let preferences = { + "locale": "en" +}; + +export const setUserPreferencesRequest = jest.fn((auth, apiUrl, object) => { + preferences = object; + return ""; // Empty string +}); + +export const getUserPreferencesRequest = jest.fn(() => { + return preferences; +}); + +export const setUserPreferenceByKeyRequest = jest.fn((auth, apiUrl, key, value) => { + preferences[key] = value; + return ""; // Empty string +}); + +export const getUserPreferenceByKeyRequest = jest.fn((auth, apiUrl, key) => { + return preferences[key]; +}); + +export const deleteUserPreferenceRequest = jest.fn((auth, apiUrl, key) => { + delete preferences[key]; + return ""; // Empty string +}); + export const createChangesetRequest = jest.fn().mockImplementation(() => 1234); export const changesetCheckRequest = jest.fn().mockImplementation(() => true); diff --git a/src/__tests__/__snapshots__/index.test.js.snap b/src/__tests__/__snapshots__/index.test.js.snap index 99003cd..a1a2dce 100644 --- a/src/__tests__/__snapshots__/index.test.js.snap +++ b/src/__tests__/__snapshots__/index.test.js.snap @@ -770,6 +770,17 @@ Object { } `; +exports[`OsmRequest deleteUserPreference Should delete a single user preference by key 1`] = `"some-value"`; + +exports[`OsmRequest deleteUserPreference Should delete a single user preference by key 2`] = `""`; + +exports[`OsmRequest deleteUserPreference Should delete a single user preference by key 3`] = ` +Object { + "some-pref-1": "some-value-1", + "some-pref-2": "some-value-2", +} +`; + exports[`OsmRequest fetchChangeset Should return changeset details 1`] = ` " @@ -3358,6 +3369,19 @@ Object { } `; +exports[`OsmRequest getAndSetAllUserPreferences Should set then get user preferences 1`] = `""`; + +exports[`OsmRequest getAndSetAllUserPreferences Should set then get user preferences 2`] = ` +Object { + "some-pref-1": "some-value-1", + "some-pref-2": "some-value-2", +} +`; + +exports[`OsmRequest getAndSetUserPreferenceByKey Should fetch a single user preference by key 1`] = `""`; + +exports[`OsmRequest getAndSetUserPreferenceByKey Should fetch a single user preference by key 2`] = `"some-value"`; + exports[`OsmRequest getRelationMembers Should return the members of a relation 1`] = ` Array [ Object { diff --git a/src/__tests__/index.test.js b/src/__tests__/index.test.js index 78c8bac..a69a31f 100644 --- a/src/__tests__/index.test.js +++ b/src/__tests__/index.test.js @@ -437,6 +437,11 @@ const sampleRelation = { const sampleRelationNoTags = JSON.parse(JSON.stringify(sampleRelation)); delete sampleRelationNoTags.tag; +const sampleUserPrefs = { + "some-pref-1": "some-value-1", + "some-pref-2": "some-value-2" +}; + describe('OsmRequest', () => { describe('Getters', () => { it('Should return a default apiUrl', () => { @@ -935,6 +940,38 @@ describe('OsmRequest', () => { }); }); + describe('getAndSetAllUserPreferences', () => { + it('Should set then get user preferences', () => { + const osm = new OsmRequest(); + const res = osm.setUserPreferences(sampleUserPrefs); + expect(res).toMatchSnapshot(); + const prefs = osm.getUserPreferences(); + expect(prefs).toMatchSnapshot(); + }); + }); + + describe('getAndSetUserPreferenceByKey', () => { + it('Should fetch a single user preference by key', () => { + const osm = new OsmRequest(); + const res = osm.setUserPreferenceByKey('some-test-preference', 'some-value'); + expect(res).toMatchSnapshot(); + const pref = osm.getUserPreferenceByKey('some-test-preference'); + expect(pref).toMatchSnapshot(); + }); + }); + + describe('deleteUserPreference', () => { + it('Should delete a single user preference by key', () => { + const osm = new OsmRequest(); + const pref = osm.getUserPreferenceByKey('some-test-preference'); + expect(pref).toMatchSnapshot(); + const res = osm.deleteUserPreference('some-test-preference'); + expect(res).toMatchSnapshot(); + const prefs = osm.getUserPreferences(); + expect(prefs).toMatchSnapshot(); + }); + }); + describe('createChangeset', () => { it('Should return the changeset ID', () => { const osm = new OsmRequest();