Skip to content

Commit 0802420

Browse files
committed
Add tests for preferences
1 parent 0170a14 commit 0802420

File tree

3 files changed

+88
-0
lines changed

3 files changed

+88
-0
lines changed

src/__mocks__/requests.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -432,6 +432,33 @@ export const fetchUserRequest = jest.fn().mockImplementation(() => ({
432432
}
433433
}));
434434

435+
let preferences = {
436+
"locale": "en"
437+
};
438+
439+
export const setUserPreferencesRequest = jest.fn((auth, apiUrl, object) => {
440+
preferences = object;
441+
return ""; // Empty string
442+
});
443+
444+
export const getUserPreferencesRequest = jest.fn(() => {
445+
return preferences;
446+
});
447+
448+
export const setUserPreferenceByKeyRequest = jest.fn((auth, apiUrl, key, value) => {
449+
preferences[key] = value;
450+
return ""; // Empty string
451+
});
452+
453+
export const getUserPreferenceByKeyRequest = jest.fn((auth, apiUrl, key) => {
454+
return preferences[key];
455+
});
456+
457+
export const deleteUserPreferenceRequest = jest.fn((auth, apiUrl, key) => {
458+
delete preferences[key];
459+
return ""; // Empty string
460+
});
461+
435462
export const createChangesetRequest = jest.fn().mockImplementation(() => 1234);
436463
export const changesetCheckRequest = jest.fn().mockImplementation(() => true);
437464

src/__tests__/__snapshots__/index.test.js.snap

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -770,6 +770,17 @@ Object {
770770
}
771771
`;
772772
773+
exports[`OsmRequest deleteUserPreference Should delete a single user preference by key 1`] = `"some-value"`;
774+
775+
exports[`OsmRequest deleteUserPreference Should delete a single user preference by key 2`] = `""`;
776+
777+
exports[`OsmRequest deleteUserPreference Should delete a single user preference by key 3`] = `
778+
Object {
779+
"some-pref-1": "some-value-1",
780+
"some-pref-2": "some-value-2",
781+
}
782+
`;
783+
773784
exports[`OsmRequest fetchChangeset Should return changeset details 1`] = `
774785
"<?xml version=\\"1.0\\" encoding=\\"UTF-8\\"?>
775786
<osm version=\\"0.6\\" generator=\\"CGImap 0.6.1 (5071 thorn-01.openstreetmap.org)\\" copyright=\\"OpenStreetMap and contributors\\" attribution=\\"http://www.openstreetmap.org/copyright\\" license=\\"http://opendatacommons.org/licenses/odbl/1-0/\\">
@@ -3358,6 +3369,19 @@ Object {
33583369
}
33593370
`;
33603371
3372+
exports[`OsmRequest getAndSetAllUserPreferences Should set then get user preferences 1`] = `""`;
3373+
3374+
exports[`OsmRequest getAndSetAllUserPreferences Should set then get user preferences 2`] = `
3375+
Object {
3376+
"some-pref-1": "some-value-1",
3377+
"some-pref-2": "some-value-2",
3378+
}
3379+
`;
3380+
3381+
exports[`OsmRequest getAndSetUserPreferenceByKey Should fetch a single user preference by key 1`] = `""`;
3382+
3383+
exports[`OsmRequest getAndSetUserPreferenceByKey Should fetch a single user preference by key 2`] = `"some-value"`;
3384+
33613385
exports[`OsmRequest getRelationMembers Should return the members of a relation 1`] = `
33623386
Array [
33633387
Object {

src/__tests__/index.test.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -437,6 +437,11 @@ const sampleRelation = {
437437
const sampleRelationNoTags = JSON.parse(JSON.stringify(sampleRelation));
438438
delete sampleRelationNoTags.tag;
439439

440+
const sampleUserPrefs = {
441+
"some-pref-1": "some-value-1",
442+
"some-pref-2": "some-value-2"
443+
};
444+
440445
describe('OsmRequest', () => {
441446
describe('Getters', () => {
442447
it('Should return a default apiUrl', () => {
@@ -935,6 +940,38 @@ describe('OsmRequest', () => {
935940
});
936941
});
937942

943+
describe('getAndSetAllUserPreferences', () => {
944+
it('Should set then get user preferences', () => {
945+
const osm = new OsmRequest();
946+
const res = osm.setUserPreferences(sampleUserPrefs);
947+
expect(res).toMatchSnapshot();
948+
const prefs = osm.getUserPreferences();
949+
expect(prefs).toMatchSnapshot();
950+
});
951+
});
952+
953+
describe('getAndSetUserPreferenceByKey', () => {
954+
it('Should fetch a single user preference by key', () => {
955+
const osm = new OsmRequest();
956+
const res = osm.setUserPreferenceByKey('some-test-preference', 'some-value');
957+
expect(res).toMatchSnapshot();
958+
const pref = osm.getUserPreferenceByKey('some-test-preference');
959+
expect(pref).toMatchSnapshot();
960+
});
961+
});
962+
963+
describe('deleteUserPreference', () => {
964+
it('Should delete a single user preference by key', () => {
965+
const osm = new OsmRequest();
966+
const pref = osm.getUserPreferenceByKey('some-test-preference');
967+
expect(pref).toMatchSnapshot();
968+
const res = osm.deleteUserPreference('some-test-preference');
969+
expect(res).toMatchSnapshot();
970+
const prefs = osm.getUserPreferences();
971+
expect(prefs).toMatchSnapshot();
972+
});
973+
});
974+
938975
describe('createChangeset', () => {
939976
it('Should return the changeset ID', () => {
940977
const osm = new OsmRequest();

0 commit comments

Comments
 (0)