Skip to content

Commit bc6bbcf

Browse files
author
Eduardo Campaña
committed
fix(facebook): sets accept header to fb request so it works on server
#9
1 parent 3ee3556 commit bc6bbcf

7 files changed

Lines changed: 94 additions & 168 deletions

File tree

src/networks/__tests__/__snapshots__/facebook.tests.js.snap

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,21 @@ exports[`Share > Facebook requestCount success 4`] = `
4343
Array [
4444
Object {
4545
"isThrow": false,
46-
"value": Promise {},
46+
"value": Object {
47+
"accept": [MockFunction] {
48+
"calls": Array [
49+
Array [
50+
"json",
51+
],
52+
],
53+
"results": Array [
54+
Object {
55+
"isThrow": false,
56+
"value": Promise {},
57+
},
58+
],
59+
},
60+
},
4761
},
4862
]
4963
`;
Lines changed: 0 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,3 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

3-
exports[`Share > Pinterest requestCount fails 1`] = `
4-
Array [
5-
Array [
6-
"https://api.pinterest.com/v1/urls/count.json?cb=cb&url=https%3A%2F%2Fdemo.frontity.com%2Fthe-beauties-of-gullfoss",
7-
],
8-
Array [
9-
"https://api.pinterest.com/v1/urls/count.json?cb=cb&url=https%3A%2F%2Fdemo.frontity.com%2Fthe-beauties-of-gullfoss",
10-
],
11-
]
12-
`;
13-
14-
exports[`Share > Pinterest requestCount success 1`] = `
15-
Array [
16-
Array [
17-
Object {
18-
"id": 60,
19-
"type": "post",
20-
},
21-
],
22-
]
23-
`;
24-
25-
exports[`Share > Pinterest requestCount success 2`] = `
26-
Array [
27-
Object {
28-
"isThrow": false,
29-
"value": "https://demo.frontity.com/the-beauties-of-gullfoss",
30-
},
31-
]
32-
`;
33-
34-
exports[`Share > Pinterest requestCount success 3`] = `
35-
Array [
36-
Array [
37-
"https://api.pinterest.com/v1/urls/count.json?cb=cb&url=https%3A%2F%2Fdemo.frontity.com%2Fthe-beauties-of-gullfoss",
38-
],
39-
]
40-
`;
41-
42-
exports[`Share > Pinterest requestCount success 4`] = `
43-
Array [
44-
Object {
45-
"isThrow": false,
46-
"value": Promise {},
47-
},
48-
]
49-
`;
50-
513
exports[`Share > Pinterest url 1`] = `"https://pinterest.com/pin/create/button?url=https%3A%2F%2Fdemo.frontity.com%2Fthe-beauties-of-gullfoss&media=https%3A%2F%2Fdemo.frontity.com%2Fwp-content%2Fuploads%2F2016%2F11%2FIceland-test-1200x726.jpg&description=Gullfoss%20is%20a%20waterfall%20located%20in%20the%20canyon%20of%20the%20Hv%C3%ADt%C3%A1%20river%20in%20southwest%20Iceland."`;

src/networks/__tests__/facebook.tests.js

Lines changed: 53 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -4,56 +4,78 @@ import Facebook from '../facebook';
44
describe('Share > Facebook', () => {
55
test('url', () => {
66
const facebook = Facebook.create({});
7-
Object.defineProperty(facebook, 'entityLink', {value: jest.fn(), writable: true});
8-
facebook.entityLink.mockReturnValueOnce('https://demo.frontity.com/the-beauty-of-gullfoss');
7+
Object.defineProperty(facebook, 'entityLink', {
8+
value: jest.fn(),
9+
writable: true,
10+
});
11+
facebook.entityLink.mockReturnValueOnce(
12+
'https://demo.frontity.com/the-beauty-of-gullfoss',
13+
);
914

10-
expect(facebook.url({ type: 'post', id: 60, quote: 'q', hashtag: '#tag' })).toMatchSnapshot();
15+
expect(
16+
facebook.url({ type: 'post', id: 60, quote: 'q', hashtag: '#tag' }),
17+
).toMatchSnapshot();
1118
});
1219

13-
test('requestCount success', done => {
14-
const request = { get: jest.fn() };
20+
test('requestCount success', async () => {
21+
const request = {
22+
get: jest.fn(() => ({
23+
accept: jest.fn().mockResolvedValueOnce({
24+
body: {
25+
share: {
26+
comment_count: 0,
27+
share_count: 123,
28+
},
29+
id: 'https://demo.frontity.com/the-beauty-of-gullfoss',
30+
},
31+
}),
32+
})),
33+
};
1534
const facebook = Facebook.create({}, { request });
16-
Object.defineProperty(facebook, 'entityLink', {value: jest.fn(), writable: true});
17-
facebook.entityLink.mockReturnValueOnce('https://demo.frontity.com/the-beauty-of-gullfoss');
18-
19-
request.get.mockResolvedValueOnce({
20-
body: {
21-
share: {
22-
comment_count: 0,
23-
share_count: 123,
24-
},
25-
id: 'https://demo.frontity.com/the-beauty-of-gullfoss',
26-
},
35+
36+
Object.defineProperty(facebook, 'entityLink', {
37+
value: jest.fn(),
38+
writable: true,
2739
});
2840

29-
when(
30-
() => facebook.count({ type: 'post', id: 60 }),
31-
() => {
32-
expect(facebook.count({ type: 'post', id: 60 })).toBe(123);
33-
expect(facebook.entityLink.mock.calls).toMatchSnapshot();
34-
expect(facebook.entityLink.mock.results).toMatchSnapshot();
35-
expect(request.get.mock.calls).toMatchSnapshot();
36-
expect(request.get.mock.results).toMatchSnapshot();
37-
done();
38-
},
41+
facebook.entityLink.mockReturnValueOnce(
42+
'https://demo.frontity.com/the-beauty-of-gullfoss',
3943
);
4044

4145
facebook.requestCount({ type: 'post', id: 60 });
46+
47+
await when(() => facebook.count({ type: 'post', id: 60 }));
48+
49+
expect(facebook.count({ type: 'post', id: 60 })).toBe(123);
50+
expect(facebook.entityLink.mock.calls).toMatchSnapshot();
51+
expect(facebook.entityLink.mock.results).toMatchSnapshot();
52+
expect(request.get.mock.calls).toMatchSnapshot();
53+
expect(request.get.mock.results).toMatchSnapshot();
4254
});
4355

4456
test('requestCount fails', async () => {
45-
const request = { get: jest.fn() };
57+
const request = {
58+
get: jest.fn(() => ({
59+
accept: jest
60+
.fn()
61+
.mockResolvedValueOnce('fail')
62+
.mockResolvedValueOnce({ bad: 'response' }),
63+
})),
64+
};
4665
const facebook = Facebook.create({}, { request });
47-
Object.defineProperty(facebook, 'entityLink', {value: jest.fn(), writable: true});
48-
facebook.entityLink.mockReturnValue('https://demo.frontity.com/the-beauty-of-gullfoss');
66+
Object.defineProperty(facebook, 'entityLink', {
67+
value: jest.fn(),
68+
writable: true,
69+
});
70+
facebook.entityLink.mockReturnValue(
71+
'https://demo.frontity.com/the-beauty-of-gullfoss',
72+
);
4973

5074
// First request (request fails)
51-
request.get.mockRejectedValueOnce('fail');
5275
await facebook.requestCount({ type: 'post', id: 60 });
5376
expect(facebook.count({ type: 'post', id: 60 })).toBe(null);
5477

5578
// Second request (bad response)
56-
request.get.mockResolvedValueOnce({ bad: 'response' });
5779
await facebook.requestCount({ type: 'post', id: 60 });
5880
expect(facebook.count({ type: 'post', id: 60 })).toBe(null);
5981

Lines changed: 9 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,64 +1,25 @@
1-
import { when } from 'mobx';
21
import Pinterest from '../pinterest';
32

43
describe('Share > Pinterest', () => {
54
test('url', () => {
65
const pinterest = Pinterest.create({});
7-
Object.defineProperty(pinterest, 'entityLink', {value: jest.fn(), writable: true});
8-
pinterest.entityLink.mockReturnValueOnce('https://demo.frontity.com/the-beauties-of-gullfoss');
6+
Object.defineProperty(pinterest, 'entityLink', {
7+
value: jest.fn(),
8+
writable: true,
9+
});
10+
pinterest.entityLink.mockReturnValueOnce(
11+
'https://demo.frontity.com/the-beauties-of-gullfoss',
12+
);
913

1014
expect(
1115
pinterest.url({
1216
type: 'post',
1317
id: 60,
14-
media: 'https://demo.frontity.com/wp-content/uploads/2016/11/Iceland-test-1200x726.jpg',
18+
media:
19+
'https://demo.frontity.com/wp-content/uploads/2016/11/Iceland-test-1200x726.jpg',
1520
description:
1621
'Gullfoss is a waterfall located in the canyon of the Hvítá river in southwest Iceland.',
1722
}),
1823
).toMatchSnapshot();
1924
});
20-
21-
test('requestCount success', done => {
22-
const request = { get: jest.fn() };
23-
const pinterest = Pinterest.create({}, { request });
24-
Object.defineProperty(pinterest, 'entityLink', {value: jest.fn(), writable: true});
25-
pinterest.entityLink.mockReturnValueOnce('https://demo.frontity.com/the-beauties-of-gullfoss');
26-
27-
request.get.mockResolvedValueOnce({
28-
text: 'cb({"url":"https://demo.frontity.com/the-beauties-of-gullfoss","count":123})',
29-
});
30-
31-
when(
32-
() => pinterest.count({ type: 'post', id: 60 }),
33-
() => {
34-
expect(pinterest.count({ type: 'post', id: 60 })).toBe(123);
35-
expect(pinterest.entityLink.mock.calls).toMatchSnapshot();
36-
expect(pinterest.entityLink.mock.results).toMatchSnapshot();
37-
expect(request.get.mock.calls).toMatchSnapshot();
38-
expect(request.get.mock.results).toMatchSnapshot();
39-
done();
40-
},
41-
);
42-
43-
pinterest.requestCount({ type: 'post', id: 60 });
44-
});
45-
46-
test('requestCount fails', async () => {
47-
const request = { get: jest.fn() };
48-
const pinterest = Pinterest.create({}, { request });
49-
Object.defineProperty(pinterest, 'entityLink', {value: jest.fn(), writable: true});
50-
pinterest.entityLink.mockReturnValue('https://demo.frontity.com/the-beauties-of-gullfoss');
51-
52-
// First request (request fails)
53-
request.get.mockRejectedValueOnce('fail');
54-
await pinterest.requestCount({ type: 'post', id: 60 });
55-
expect(pinterest.count({ type: 'post', id: 60 })).toBe(null);
56-
57-
// Second request (bad response)
58-
request.get.mockResolvedValueOnce({ bad: 'response' });
59-
await pinterest.requestCount({ type: 'post', id: 60 });
60-
expect(pinterest.count({ type: 'post', id: 60 })).toBe(null);
61-
62-
expect(request.get.mock.calls).toMatchSnapshot();
63-
});
6425
});

src/networks/all.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@ export default Network.named('All')
2121
requestCount: flow(function* requestAllCounts({ type, id }) {
2222
yield Promise.all(
2323
getParent(self)
24-
.networks.filter(network => typeof network.requestCount === 'function')
24+
.networks.filter(
25+
network => typeof network.requestCount === 'function',
26+
)
2527
.map(network => network.requestCount({ type, id })),
2628
);
2729
}),

src/networks/facebook.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,11 @@ export default Network.named('Facebook')
2626

2727
let count;
2828
try {
29-
const response = yield getEnv(self).request.get(requestUrl);
29+
const response = yield getEnv(self)
30+
.request.get(requestUrl)
31+
.accept('json');
3032
count = response.body.share.share_count;
3133
} catch (error) {
32-
console.warn(`share.facebook.requestCount failed`, error);
3334
count = null;
3435
}
3536

src/networks/pinterest.js

Lines changed: 11 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,14 @@
1-
import { getEnv, flow } from 'mobx-state-tree';
2-
import { set } from 'mobx';
31
import { format } from 'url';
42
import Network from './network';
53

6-
export default Network.named('Pinterest')
7-
.views(self => ({
8-
url({ type, id, media, description }) {
9-
const url = self.entityLink({ type, id });
10-
return format({
11-
protocol: 'https:',
12-
host: 'pinterest.com',
13-
pathname: 'pin/create/button',
14-
query: { url, media, description },
15-
});
16-
},
17-
}))
18-
.actions(self => ({
19-
requestCount: flow(function* requestCountPinterest({ type, id }) {
20-
const url = self.entityLink({ type, id });
21-
const requestUrl = format({
22-
protocol: 'https:',
23-
host: 'api.pinterest.com',
24-
pathname: 'v1/urls/count.json',
25-
query: { cb: 'cb', url },
26-
});
27-
28-
let count;
29-
try {
30-
const response = yield getEnv(self).request.get(requestUrl);
31-
const data = /\((.+)\)/.exec(response.text)[1];
32-
({ count } = JSON.parse(data));
33-
} catch (error) {
34-
console.warn('shared.pinterest.requestCount', error);
35-
count = null;
36-
}
37-
38-
set(self.countsMap, { [`${type}_${id}`]: count });
39-
}),
40-
}));
4+
export default Network.named('Pinterest').views(self => ({
5+
url({ type, id, media, description }) {
6+
const url = self.entityLink({ type, id });
7+
return format({
8+
protocol: 'https:',
9+
host: 'pinterest.com',
10+
pathname: 'pin/create/button',
11+
query: { url, media, description },
12+
});
13+
},
14+
}));

0 commit comments

Comments
 (0)