Skip to content

Commit

Permalink
added tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mattiapezzotti committed Aug 19, 2024
1 parent 397ac82 commit a773d0a
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 102 deletions.
30 changes: 15 additions & 15 deletions modules/services/panoramax.js
Original file line number Diff line number Diff line change
Expand Up @@ -194,18 +194,6 @@ function loadTileDataToCache(data, tile, zoom) {
}
}

async function getImageData(collection_id, image_id){
const requestUrl = imageDataUrl.replace('{collectionId}', collection_id)
.replace('{itemId}', image_id);

const response = await fetch(requestUrl, { method: 'GET' });
if (!response.ok) {
throw new Error(response.status + ' ' + response.statusText);
}
const data = await response.json();
return data;
}

async function getUsername(user_id){
const requestUrl = usernameURL.replace('{userId}', user_id);

Expand Down Expand Up @@ -310,7 +298,7 @@ export default {

// Set the currently visible image
setActiveImage: function(image) {
if (image) {
if (image && image.id && image.sequence_id) {
_activeImage = {
id: image.id,
sequence_id: image.sequence_id
Expand Down Expand Up @@ -449,7 +437,7 @@ export default {
.attr('href', viewerLink)
.text('panoramax.xyz');

getImageData(d.sequence_id, d.id).then(function(data){
this.getImageData(d.sequence_id, d.id).then(function(data){
_currentScene = {
currentImage: null,
nextImage: null,
Expand Down Expand Up @@ -514,6 +502,18 @@ export default {
return _currentFrame;
},

getImageData: async function(collection_id, image_id){
const requestUrl = imageDataUrl.replace('{collectionId}', collection_id)
.replace('{itemId}', image_id);

const response = await fetch(requestUrl, { method: 'GET' });
if (!response.ok) {
throw new Error(response.status + ' ' + response.statusText);
}
const data = await response.json();
return data;
},

ensureViewerLoaded: function(context) {

let that = this;
Expand Down Expand Up @@ -618,7 +618,7 @@ export default {
.classed('hide', true);
context.container().selectAll('.viewfield-group, .sequence, .icon-sign')
.classed('currentView', false);
this.setActiveImage();
this.setActiveImage(null);
return this.setStyles(context, null);
},

Expand Down
147 changes: 60 additions & 87 deletions test/spec/services/panoramax.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,39 @@
describe('iD.servicePanoramax', function() {
var dimensions = [64, 64];
const dimensions = [64, 64];
var context, panoramax;
const data = {
images:[{
loc: [10,0],
capture_time: '2020-01-01',
id: 'abc',
account_id: '123',
sequence_id: 'a1b2',
heading: 0,
image_path: '',
isPano: true,
model: 'camera',
}, {
loc: [10,1],
capture_time: '2020-02-01',
id: 'def',
account_id: 'c3d4',
sequence_id: '',
heading: 0,
image_path: '',
isPano: true,
model: 'camera',
}, {
loc: [10,2],
capture_time: '2020-02-01',
id: 'ghi',
account_id: '789',
sequence_id: 'e5f6',
heading: 0,
image_path: '',
isPano: true,
model: 'camera',
}],
};

before(function() {
iD.services.panoramax = iD.servicePanoramax;
Expand Down Expand Up @@ -52,92 +85,7 @@ describe('iD.servicePanoramax', function() {
});

describe('#loadImages', function() {
it('fires loadedImages when images are loaded', function(done) {
var data = {
images:[{
loc: [10,0],
capture_time: '2020-01-01',
id: 'abc',
account_id: '123',
sequence_id: 'a1b2',
heading: 0,
image_path: '',
isPano: true,
model: 'camera',
}, {
loc: [10,1],
capture_time: '2020-02-01',
id: 'def',
account_id: 'c3d4',
sequence_id: '',
heading: 0,
image_path: '',
isPano: true,
model: 'camera',
}, {
loc: [10,2],
capture_time: '2020-02-01',
id: 'ghi',
account_id: '789',
sequence_id: 'e5f6',
heading: 0,
image_path: '',
isPano: true,
model: 'camera',
}],
};

fetchMock.mock(new RegExp('/panoramax-test/'), {
body: JSON.stringify(data),
status: 200,
headers: { 'Content-Type': 'application/json' }
});

context.projection.scale(iD.geoZoomToScale(15));

panoramax.on('loadedImages', function() {
expect(fetchMock.calls().length).to.eql(1); // 1 nearby-photos
done();
}).catch(done());

panoramax.loadImages(context.projection);
});

it('does not load images around null island', function (done) {
var data = {
images:[{
loc: [10,0],
capture_time: '2020-01-01',
id: 'abc',
account_id: '123',
sequence_id: 'a1b2',
heading: 0,
image_path: '',
isPano: true,
model: 'camera',
}, {
loc: [10,1],
capture_time: '2020-02-01',
id: 'def',
account_id: 'c3d4',
sequence_id: '',
heading: 0,
image_path: '',
isPano: true,
model: 'camera',
}, {
loc: [10,2],
capture_time: '2020-02-01',
id: 'ghi',
account_id: '789',
sequence_id: 'e5f6',
heading: 0,
image_path: '',
isPano: true,
model: 'camera',
}],
};

var spy = sinon.spy();
fetchMock.mock(new RegExp('/panoramax-test/'), {
body: JSON.stringify(data),
Expand All @@ -158,6 +106,14 @@ describe('iD.servicePanoramax', function() {
done();
}, 200);
});

it('handle API error response', function(done) {
fetchMock.mock('/panoramax-test/', 500);

panoramax.getImageData('collection1', 'image1')
.then(() => done(new Error('Expected method to reject.')))
.catch(() => done());
});
});

describe('#images', function() {
Expand Down Expand Up @@ -191,6 +147,24 @@ describe('iD.servicePanoramax', function() {
var res = panoramax.images(context.projection);
expect(res).to.have.length.of.at.most(5);
});

it('handle invalid image data', function() {
const invalidImage = { id: null, sequence_id: null };
panoramax.setActiveImage(invalidImage);
expect(panoramax.getActiveImage()).to.be.null;
});

it('return empty array when no images are available', function() {
const result = panoramax.images(context.projection);
expect(result).to.deep.equal([]);
});

it('load images quickly under normal conditions', function() {
const start = performance.now();
panoramax.loadImages(context.projection);
const duration = performance.now() - start;
expect(duration).to.be.lessThan(1000);
});
});


Expand Down Expand Up @@ -220,5 +194,4 @@ describe('iD.servicePanoramax', function() {
expect(panoramax.getActiveImage()).to.eql(d);
});
});

});

0 comments on commit a773d0a

Please sign in to comment.