Skip to content

Commit

Permalink
Merge pull request #177 from Yoctol/return-null
Browse files Browse the repository at this point in the history
return null when profile setting does not exist
  • Loading branch information
chentsulin committed Oct 26, 2017
2 parents 0decc07 + dd3b5cb commit 2d846b7
Show file tree
Hide file tree
Showing 3 changed files with 174 additions and 22 deletions.
4 changes: 2 additions & 2 deletions packages/messaging-api-messenger/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1241,7 +1241,7 @@ Retrieves the current value of one or more Messenger Profile properties by name.

Param | Type | Description
------ | --------------- | -----------
fields | `Array<String>` | Value must be among `account_linking_url | persistent_menu | get_started | greeting | whitelisted_domains | payment_settings | target_audience | home_url`.
fields | `Array<String>` | Value must be among `account_linking_url`, `persistent_menu`, `get_started`, `greeting`, `whitelisted_domains`, `payment_settings`, `target_audience`, `home_url`.

Example:
```js
Expand Down Expand Up @@ -1312,7 +1312,7 @@ Deletes one or more Messenger Profile properties. Only properties specified in t

Param | Type | Description
------ | --------------- | -----------
fields | `Array<String>` | Value must be among `account_linking_url | persistent_menu | get_started | greeting | whitelisted_domains | payment_settings | target_audience | home_url`.
fields | `Array<String>` | Value must be among `account_linking_url`, `persistent_menu`, `get_started`, `greeting`, `whitelisted_domains`, `payment_settings`, `target_audience`, `home_url`.

Example:
```js
Expand Down
52 changes: 32 additions & 20 deletions packages/messaging-api-messenger/src/MessengerClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -149,10 +149,12 @@ export default class MessengerClient {
*
* https://developers.facebook.com/docs/messenger-platform/messenger-profile/get-started-button
*/
getGetStarted = (): Promise<MessengerProfileResponse> =>
this.getMessengerProfile(['get_started']).then(res => res[0].get_started);
getGetStarted = (): Promise<MessengerProfileResponse | null> =>
this.getMessengerProfile(['get_started']).then(
res => (res[0] ? res[0].get_started : null)
);

getGetStartedButton = (): Promise<MessengerProfileResponse> => {
getGetStartedButton = (): Promise<MessengerProfileResponse | null> => {
warning(
false,
'`getGetStartedButton` is deprecated, use `getGetStarted` instead'
Expand Down Expand Up @@ -191,9 +193,9 @@ export default class MessengerClient {
*
* https://developers.facebook.com/docs/messenger-platform/messenger-profile/persistent-menu
*/
getPersistentMenu = (): Promise<MessengerProfileResponse> =>
getPersistentMenu = (): Promise<MessengerProfileResponse | null> =>
this.getMessengerProfile(['persistent_menu']).then(
res => res[0].persistent_menu
res => (res[0] ? res[0].persistent_menu : null)
);

setPersistentMenu = (
Expand Down Expand Up @@ -227,10 +229,12 @@ export default class MessengerClient {
*
* https://developers.facebook.com/docs/messenger-platform/messenger-profile/greeting-text
*/
getGreeting = (): Promise<MessengerProfileResponse> =>
this.getMessengerProfile(['greeting']).then(res => res[0].greeting);
getGreeting = (): Promise<MessengerProfileResponse | null> =>
this.getMessengerProfile(['greeting']).then(
res => (res[0] ? res[0].greeting : null)
);

getGreetingText = (): Promise<MessengerProfileResponse> => {
getGreetingText = (): Promise<MessengerProfileResponse | null> => {
warning(
false,
'`getGreetingText` is deprecated, use `getGreeting` instead'
Expand Down Expand Up @@ -283,7 +287,7 @@ export default class MessengerClient {
*
* https://developers.facebook.com/docs/messenger-platform/messenger-profile/domain-whitelisting
*/
getDomainWhitelist = (): Promise<MessengerProfileResponse> => {
getDomainWhitelist = (): Promise<MessengerProfileResponse | null> => {
warning(
false,
'`getDomainWhitelist` is deprecated. use `getWhitelistedDomains` instead.'
Expand Down Expand Up @@ -314,9 +318,9 @@ export default class MessengerClient {
*
* https://developers.facebook.com/docs/messenger-platform/messenger-profile/domain-whitelisting
*/
getWhitelistedDomains = (): Promise<MessengerProfileResponse> =>
getWhitelistedDomains = (): Promise<MessengerProfileResponse | null> =>
this.getMessengerProfile(['whitelisted_domains']).then(
res => res[0].whitelisted_domains
res => (res[0] ? res[0].whitelisted_domains : null)
);

setWhitelistedDomains = (
Expand All @@ -334,8 +338,10 @@ export default class MessengerClient {
*
* https://developers.facebook.com/docs/messenger-platform/messenger-profile/account-linking-url
*/
getAccountLinkingURL = (): Promise<MessengerProfileResponse> =>
this.getMessengerProfile(['account_linking_url']).then(res => res[0]);
getAccountLinkingURL = (): Promise<MessengerProfileResponse | null> =>
this.getMessengerProfile(['account_linking_url']).then(
res => (res[0] ? res[0] : null)
);

setAccountLinkingURL = (url: string): Promise<MutationSuccessResponse> =>
this.setMessengerProfile({
Expand All @@ -350,8 +356,10 @@ export default class MessengerClient {
*
* https://developers.facebook.com/docs/messenger-platform/messenger-profile/payment-settings
*/
getPaymentSettings = (): Promise<MessengerProfileResponse> =>
this.getMessengerProfile(['payment_settings']).then(res => res[0]);
getPaymentSettings = (): Promise<MessengerProfileResponse | null> =>
this.getMessengerProfile(['payment_settings']).then(
res => (res[0] ? res[0] : null)
);

setPaymentPrivacyPolicyURL = (
url: string
Expand Down Expand Up @@ -386,8 +394,10 @@ export default class MessengerClient {
*
* https://developers.facebook.com/docs/messenger-platform/messenger-profile/target-audience
*/
getTargetAudience = (): Promise<MessengerProfileResponse> =>
this.getMessengerProfile(['target_audience']).then(res => res[0]);
getTargetAudience = (): Promise<MessengerProfileResponse | null> =>
this.getMessengerProfile(['target_audience']).then(
res => (res[0] ? res[0] : null)
);

setTargetAudience = (
type: string,
Expand All @@ -412,10 +422,12 @@ export default class MessengerClient {
*
* https://developers.facebook.com/docs/messenger-platform/messenger-profile/home-url
*/
getHomeURL = (): Promise<MessengerProfileResponse> =>
this.getMessengerProfile(['home_url']).then(res => res[0]);
getHomeURL = (): Promise<MessengerProfileResponse | null> =>
this.getMessengerProfile(['home_url']).then(
res => (res[0] ? res[0] : null)
);

getChatExtensionHomeURL = (): Promise<MessengerProfileResponse> => {
getChatExtensionHomeURL = (): Promise<MessengerProfileResponse | null> => {
warning(
false,
'`getChatExtensionHomeURL` is deprecated. use `getHomeURL` instead.'
Expand Down
140 changes: 140 additions & 0 deletions packages/messaging-api-messenger/src/__tests__/MessengerClient.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,24 @@ describe('get started button', () => {
payload: 'GET_STARTED',
});
});

it('should response null when data is an empty array', async () => {
const { client, mock } = createMock();

const reply = {
data: [],
};

mock
.onGet(
`/me/messenger_profile?fields=get_started&access_token=${ACCESS_TOKEN}`
)
.reply(200, reply);

const res = await client.getGetStartedButton();

expect(res).toEqual(null);
});
});

describe('#setGetStartedButton', () => {
Expand Down Expand Up @@ -396,6 +414,24 @@ describe('persistent menu', () => {
},
]);
});

it('should response null when data is an empty array', async () => {
const { client, mock } = createMock();

const reply = {
data: [],
};

mock
.onGet(
`/me/messenger_profile?fields=persistent_menu&access_token=${ACCESS_TOKEN}`
)
.reply(200, reply);

const res = await client.getPersistentMenu();

expect(res).toEqual(null);
});
});

describe('#setPersistentMenu', () => {
Expand Down Expand Up @@ -760,6 +796,20 @@ describe('greeting text', () => {
},
]);
});

it('should response null when data is an empty array', async () => {
const { client, mock } = createMock();

const reply = {
data: [],
};

mock.onGet().reply(200, reply);

const res = await client.getGreetingText();

expect(res).toEqual(null);
});
});

describe('#setGreetingText', () => {
Expand Down Expand Up @@ -867,6 +917,24 @@ describe('whitelisted domains', () => {

expect(res).toEqual(['http://www.yoctol.com/']);
});

it('should response null when data is an empty array', async () => {
const { client, mock } = createMock();

const reply = {
data: [],
};

mock
.onGet(
`/me/messenger_profile?fields=whitelisted_domains&access_token=${ACCESS_TOKEN}`
)
.reply(200, reply);

const res = await client.getWhitelistedDomains();

expect(res).toEqual(null);
});
});

describe('#setWhitelistedDomains', () => {
Expand Down Expand Up @@ -937,6 +1005,24 @@ describe('account linking url', () => {
'https://www.example.com/oauth?response_type=code&client_id=1234567890&scope=basic',
});
});

it('should response null when data is an empty array', async () => {
const { client, mock } = createMock();

const reply = {
data: [],
};

mock
.onGet(
`/me/messenger_profile?fields=account_linking_url&access_token=${ACCESS_TOKEN}`
)
.reply(200, reply);

const res = await client.getAccountLinkingURL();

expect(res).toEqual(null);
});
});

describe('#setAccountLinkingURL', () => {
Expand Down Expand Up @@ -1012,6 +1098,24 @@ describe('payment settings', () => {
test_users: ['12345678'],
});
});

it('should response null when data is an empty array', async () => {
const { client, mock } = createMock();

const reply = {
data: [],
};

mock
.onGet(
`/me/messenger_profile?fields=payment_settings&access_token=${ACCESS_TOKEN}`
)
.reply(200, reply);

const res = await client.getPaymentSettings();

expect(res).toEqual(null);
});
});

describe('#setPaymentPrivacyPolicyURL', () => {
Expand Down Expand Up @@ -1134,6 +1238,24 @@ describe('target audience', () => {
},
});
});

it('should response null when data is an empty array', async () => {
const { client, mock } = createMock();

const reply = {
data: [],
};

mock
.onGet(
`/me/messenger_profile?fields=target_audience&access_token=${ACCESS_TOKEN}`
)
.reply(200, reply);

const res = await client.getTargetAudience();

expect(res).toEqual(null);
});
});

describe('#setTargetAudience', () => {
Expand Down Expand Up @@ -1240,6 +1362,24 @@ describe('chat extension home URL', () => {
in_test: true,
});
});

it('should response null when data is an empty array', async () => {
const { client, mock } = createMock();

const reply = {
data: [],
};

mock
.onGet(
`/me/messenger_profile?fields=home_url&access_token=${ACCESS_TOKEN}`
)
.reply(200, reply);

const res = await client.getChatExtensionHomeURL();

expect(res).toEqual(null);
});
});

describe('#setChatExtensionHomeURL', () => {
Expand Down

0 comments on commit 2d846b7

Please sign in to comment.