Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
'/test/succeeds/json-one',
() =>
new Promise(resolve => {
resolve(jsonResponse());
})
);
fetchMock.get(
'/test/succeeds/json-two',
() =>
new Promise(resolve => {
resolve(jsonResponse2());
})
);
fetchMock.patch(
'/test/succeeds/patch',
() =>
new Promise(resolve => {
resolve(jsonResponse3());
})
);
// We do this at the start of each test, just in case a test
// replaces the global fetch and does not reset it
beforeEach(() => {
fetchMock.reset();
});
remoteUrl: getTestURL('testURL'),
ot: true,
localVersionPath,
remoteVersionPath,
onReconnectionCountdown,
onReconnectionEnd,
useWebSocket: true,
pingIntervalS
});
fetchMock.patch(getTestURL('testURL'), {
status: 200,
headers: { contentType: 'application/json-patch+json' },
body: '[]'
}, {name: 'ping'});
fetchMock.patch(getTestURL('testURL')+'/reconnect', {
status: 200,
headers: { contentType: 'application/json' },
body: `{"witaj": "swiecie", "${remoteVersion}": 777}`
}, {name: 'reconnect'});
// wait for HTTP responses and WebSocket server to be established
await sleep();
// issue a change that will not get acknoledged by server
palindrom.obj.hello = 'OT';
// close the WS
mockSocketServer.close({
code,
wasClean,
reason
});
// setup another web socket so reconnection would not throw an error
anotherSocket = new MockSocketServer(getTestURL('testURL', false, true));
}, {name: 'establish'});
onReconnectionCountdown = sinon.spy();
onReconnectionEnd = sinon.spy();
palindrom = new Palindrom({
remoteUrl: getTestURL('testURL'),
ot: true,
localVersionPath,
remoteVersionPath,
onReconnectionCountdown,
onReconnectionEnd,
useWebSocket: true,
pingIntervalS
});
fetchMock.patch(getTestURL('testURL'), {
status: 200,
headers: { contentType: 'application/json-patch+json' },
body: '[]'
}, {name: 'ping'});
fetchMock.patch(getTestURL('testURL')+'/reconnect', {
status: 200,
headers: { contentType: 'application/json' },
body: `{"witaj": "swiecie", "${remoteVersion}": 777}`
}, {name: 'reconnect'});
// wait for HTTP responses and WebSocket server to be established
await sleep();
// issue a change that will not get acknoledged by server
palindrom.obj.hello = 'OT';
// close the WS
mockSocketServer.close({
code,
it(`'updateNewVersionChecker' should update value on backend`, async () => {
const mock = fetchMock.patch(`/api/settings/new-version-checker`, {status: 204})
await api.settings.updateNewVersionChecker({enabled: false, include_prerelease: true, interval: 60})
expect(mock.called())
expect(JSON.parse(mock.lastCall()[1].body)).to.be.eql({enabled: false, include_prerelease: true, interval: 3600})
})
it('supports PATCH requests', async () => {
expect.assertions(2);
const link = new JsonApiLink({ uri: '/api' });
const post = {
data: {
type: 'posts',
id: '1',
attributes: { title: 'Love apollo', categoryId: 6 },
},
};
fetchMock.patch('/api/posts/1', post);
const resultPost = {
__typename: 'posts',
id: '1',
title: 'Love apollo',
categoryId: 6,
};
const editPostMutation = gql`
fragment PartialPostInput on REST {
id: ID
title: String
categoryId: Number
}
mutation editPost($id: ID!, $input: PartialPostInput!) {
editedPost(id: $id, input: $input)
body: '{"hello": "world"}'
}, {name: 'establish'});
onReconnectionCountdown = sinon.spy();
onReconnectionEnd = sinon.spy();
palindrom = new Palindrom({
remoteUrl: getTestURL('testURL'),
ot: true,
localVersionPath,
remoteVersionPath,
onReconnectionCountdown,
onReconnectionEnd,
pingIntervalS
});
fetchMock.patch(getTestURL('testURL'), {
status: 200,
headers: { contentType: 'application/json-patch+json' },
body: '[]'
}, {
name: 'ping',
functionMatcher: function(url, options){
return options.body === '[]';
}
});
fetchMock.patch(getTestURL('testURL'), {
status: 200,
headers: { contentType: 'application/json-patch+json' },
body: `[
{"op": "replace", "path": "${remoteVersionPath}", "value": 100},
{"op": "test", "path": "${localVersionPath}", "value": 1}
]`
it('should handle a failure to update an entry from client to server', (done) => {
fetchMock.get('/api/entries/sync/1234', {
status: 200,
body: {
entries: [],
timestamp: 4321
}
});
fetchMock.patch('/api/entry/0', 500);
el.state.loggedIn = true;
el.state.timestamp = 1234;
el.state.entries = [ { id: 0, date: '2017-01-01', text: 'what', needsSync: true } ];
Entry.getEntries(el);
setTimeout(() => {
expect(console.log.calledWith('updateEntryFailure')).to.be.true;
done();
});
});
it('should sync entries from the client to the server when one or more entries has the needsSync flag', (done) => {
fetchMock.get('/api/entries/sync/1234', {
status: 200,
body: {
entries: [],
timestamp: 4321
}
});
fetchMock.post('/api/entry', {
status: 200,
body: { id: 3 }
});
fetchMock.patch('/api/entry/1', 204);
fetchMock.delete('/api/entry/2', 204);
el.state.loggedIn = true;
el.state.timestamp = 1234;
el.state.entries = [
{ id: 0, date: '2018-01-01', text: 'bogus', newEntry: true, needsSync: true },
{ id: 1, date: '2017-01-01', text: 'what', needsSync: true },
{ id: 2, deleted: true, needsSync: true },
];
Entry.getEntries(el);
setTimeout(() => {
const entries = el.state.entries;
expect(entries.length).to.equal(2);
expect(entries[0].id).to.equal(3);
expect(entries[0].newEntry).to.be.undefined;
expect(entries[0].needsSync).to.be.undefined;
it('should set state.entry.text, mark state.entries[entryIndex] for update, and remove the needsSync flag when the network call succeedes', (done) => {
fetchMock.patch('/api/entry/0', 204);
const updateObj = {
entry: { text: 'b' },
property: 'text',
entryId: 0
};
Entry.updateEntry(el, updateObj);
const firstCallArgs = el.set.args[0];
expect(firstCallArgs[0].entry.text).to.equal(updateObj.entry.text);
expect(firstCallArgs[0].entry.needsSync).to.be.true;
expect(firstCallArgs[0].entries[0].text).to.equal(updateObj.entry.text);
expect(firstCallArgs[0].entries[0].needsSync).to.be.true;
setTimeout(() => {
const secondCallArgs = el.set.args[1];
expect(secondCallArgs[0].entry.text).to.equal(updateObj.entry.text);
it('should call the update endpoint with an entry object', (done) => {
fetchMock.patch('/api/entry/1234', 204);
Entry.update(1234, fakeEntry).then(() => {
const options = fetchMock.lastOptions();
expect(typeof options.body).to.equal('string');
expect(JSON.parse(options.body).date).to.equal('1234');
done();
}).catch(done);
});