Skip to content

Commit

Permalink
Add tests for preferences
Browse files Browse the repository at this point in the history
  • Loading branch information
Binnette committed Jul 22, 2024
1 parent 0170a14 commit 0802420
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 0 deletions.
27 changes: 27 additions & 0 deletions src/__mocks__/requests.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
24 changes: 24 additions & 0 deletions src/__tests__/__snapshots__/index.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -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`] = `
"<?xml version=\\"1.0\\" encoding=\\"UTF-8\\"?>
<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/\\">
Expand Down Expand Up @@ -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 {
Expand Down
37 changes: 37 additions & 0 deletions src/__tests__/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down Expand Up @@ -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();
Expand Down

0 comments on commit 0802420

Please sign in to comment.