Skip to content

Commit

Permalink
feat(openwith): Box Edit SFC support
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeremy Press authored Jan 3, 2019
1 parent 3746334 commit f19158d
Show file tree
Hide file tree
Showing 6 changed files with 259 additions and 58 deletions.
32 changes: 30 additions & 2 deletions src/api/OpenWith.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,13 @@
*/

import Base from './Base';
import { HEADER_ACCEPT_LANGUAGE, DEFAULT_LOCALE, ERROR_CODE_FETCH_INTEGRATIONS } from '../constants';
import {
HEADER_ACCEPT_LANGUAGE,
DEFAULT_LOCALE,
ERROR_CODE_FETCH_INTEGRATIONS,
BOX_EDIT_INTEGRATION_ID,
BOX_EDIT_SFC_INTEGRATION_ID,
} from '../constants';

class OpenWith extends Base {
/**
Expand Down Expand Up @@ -49,12 +55,34 @@ class OpenWith extends Base {
params,
successCallback: openWithIntegrations => {
const formattedOpenWithData = this.formatOpenWithData(openWithIntegrations);
successCallback(formattedOpenWithData);
const consolidatedOpenWithIntegrations = this.consolidateBoxEditIntegrations(formattedOpenWithData);
successCallback(consolidatedOpenWithIntegrations);
},
errorCallback,
});
}

/**
* Removes the Box Edit SFC integration if the higher scoped Box Edit integration is present.
* Box Edit and SFC Box Edit are considered separate integrations by the API. We only want to show one,
* even if both are enabled and returned from the API.
*
* @param {Array<Integration>} integrations - List of integrations
* @return {Array<Integration>} Integrations with only one Box Edit integration
*/
consolidateBoxEditIntegrations(integrations: Array<Integration>): Array<Integration> {
let consolidatedIntegrations = [...integrations];
const boxEditIntegration = integrations.some(item => item.appIntegrationId === BOX_EDIT_INTEGRATION_ID);

if (boxEditIntegration) {
consolidatedIntegrations = integrations.filter(
item => item.appIntegrationId !== BOX_EDIT_SFC_INTEGRATION_ID,
);
}

return consolidatedIntegrations;
}

/**
* Formats Open With data conveniently for the client
*
Expand Down
187 changes: 150 additions & 37 deletions src/api/__tests__/MockOpenWithData.json
Original file line number Diff line number Diff line change
@@ -1,47 +1,160 @@
{
"is_disabled": false,
"icon": "data:image/svg+xml;base64,PHN2ZyB2ZXJzaW9uPSIxLjEiIHByZXNlcnZlQXNwZWN0UmF0aW89InhNaWRZTWlkIG1lZXQiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgeG1sbnM6eGxpbms9Imh0dHA6Ly93d3cudzMub3JnLzE5OTkveGxpbmsiIHZpZXdCb3g9IjAgMCAzMiAzMiIgd2lkdGg9IjMyIiBoZWlnaHQ9IjMyIiBlbmFibGUtYmFja2dyb3VuZD0ibmV3IDAgMCAzMiAzMiIgeG1sOnNwYWNlPSJwcmVzZXJ2ZSIgcm9sZT0iaW1nIiA+ICAgICAgIDxwYXRoIGQ9Ik0yNSAyN0g3VjVoMTNsNSA1djE3eiIgZmlsbD0iI0ZGRkZGRiIvPiAgICA8cGF0aCBkPSJNMjAgNEg3Yy0uNiAwLTEgLjQtMSAxdjIyYzAgLjYuNCAxIDEgMWgxOGMuNiAwIDEtLjQgMS0xVjkuOUwyMCA0em01IDIzSDdWNWgxM3Y0YzAgLjYuNCAxIDEgMWg0djE3eiIgZmlsbD0iIzQwODNGNyIvPiAgICA8cGF0aCBkPSJNMTAgMTVoMTF2MUgxMHYtMXptMCAyaDExdjFIMTB2LTF6bTAgMmgxMXYxSDEwdi0xem0wIDJoOHYxaC04di0xeiIgZmlsbD0iIzQwODNGNyIvPjwvc3ZnPg==",
"disabled_reasons": [],
"default_app_integration": {
"id": "3",
"type": "app_integration"
"default": {
"is_disabled": false,
"icon": "data:image/svg+xml;base64,PHN2ZyB2ZXJzaW9uPSIxLjEiIHByZXNlcnZlQXNwZWN0UmF0aW89InhNaWRZTWlkIG1lZXQiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgeG1sbnM6eGxpbms9Imh0dHA6Ly93d3cudzMub3JnLzE5OTkveGxpbmsiIHZpZXdCb3g9IjAgMCAzMiAzMiIgd2lkdGg9IjMyIiBoZWlnaHQ9IjMyIiBlbmFibGUtYmFja2dyb3VuZD0ibmV3IDAgMCAzMiAzMiIgeG1sOnNwYWNlPSJwcmVzZXJ2ZSIgcm9sZT0iaW1nIiA+ICAgICAgIDxwYXRoIGQ9Ik0yNSAyN0g3VjVoMTNsNSA1djE3eiIgZmlsbD0iI0ZGRkZGRiIvPiAgICA8cGF0aCBkPSJNMjAgNEg3Yy0uNiAwLTEgLjQtMSAxdjIyYzAgLjYuNCAxIDEgMWgxOGMuNiAwIDEtLjQgMS0xVjkuOUwyMCA0em01IDIzSDdWNWgxM3Y0YzAgLjYuNCAxIDEgMWg0djE3eiIgZmlsbD0iIzQwODNGNyIvPiAgICA8cGF0aCBkPSJNMTAgMTVoMTF2MUgxMHYtMXptMCAyaDExdjFIMTB2LTF6bTAgMmgxMXYxSDEwdi0xem0wIDJoOHYxaC04di0xeiIgZmlsbD0iIzQwODNGNyIvPjwvc3ZnPg==",
"disabled_reasons": [],
"default_app_integration": {
"id": "3",
"type": "app_integration"
},
"items": [
{
"display_name": "Adobe Sign",
"display_description": "Send for Signature",
"display_order": 1,
"is_disabled": false,
"should_show_consent_popup": false,
"disabled_reasons": [],
"app_integration": {
"id": "3282",
"type": "app_integration"
}
},
{
"display_name": "Google Docs",
"display_description": "Open with Google Docs",
"display_order": 3,
"is_disabled": true,
"disabled_reasons": ["manually disabled"],
"should_show_consent_popup": false,
"app_integration": {
"id": "10897",
"type": "app_integration"
}
},
{
"display_name": "Google Slides",
"display_description": "Open with Google Slides",
"display_order": 2,
"is_disabled": false,
"disabled_reasons": [],
"should_show_consent_popup": false,
"app_integration": {
"id": "10897",
"type": "app_integration"
}
}
]
},
"items": [
{
"display_name": "Adobe Sign",
"display_description": "Send for Signature",
"display_order": 1,
"is_disabled": false,
"should_show_consent_popup": false,
"disabled_reasons": [],
"app_integration": {
"id": "3282",
"defaultAsIntegrations": [
[
{
"displayName": "Adobe Sign",
"displayDescription": "Send for Signature",
"displayOrder": 1,
"isDefault": true,
"isDisabled": false,
"requiresConsent": false,
"DisabledReasons": [],
"appIntegrationId": "3282",
"type": "app_integration"
},
{
"displayName": "Google Docs",
"displayDescription": "Open with Google Docs",
"displayOrder": 3,
"isDisabled": true,
"DisabledReasons": ["manually disabled"],
"requiresConsent": false,
"appIntegrationId": "10897",
"type": "app_integration"
},
{
"displayName": "Google Slides",
"displayDescription": "Open with Google Slides",
"displayOrder": 2,
"isDisabled": false,
"DisabledReasons": [],
"requiresConsent": false,
"appIntegrationId": "10897",
"type": "app_integration"
}
]
],
"boxEdit": [
{
"displayName": "Open",
"displayDescription": "Open this file on your desktop",
"displayOrder": 2,
"isDisabled": false,
"requiresConsent": false,
"DisabledReasons": [],
"appIntegrationId": "1338",
"type": "app_integration"
},
{
"display_name": "Google Docs",
"display_description": "Open with Google Docs",
"display_order": 3,
"is_disabled": true,
"disabled_reasons": ["manually disabled"],
"should_show_consent_popup": false,
"app_integration": {
"id": "10897",
"type": "app_integration"
}
"displayName": "Google Docs",
"displayDescription": "Open with Google Docs",
"displayOrder": 3,
"isDisabled": true,
"DisabledReasons": ["manually disabled"],
"requiresConsent": false,
"appIntegrationId": "10897",
"type": "app_integration"
}
],
"boxEditSFC": [
{
"displayName": "Open",
"displayDescription": "Open this file on your desktop",
"displayOrder": 1,
"isDisabled": false,
"requiresConsent": false,
"DisabledReasons": [],
"appIntegrationId": "13418",
"type": "app_integration"
},
{
"display_name": "Google Slides",
"display_description": "Open with Google Slides",
"display_order": 2,
"is_disabled": false,
"disabled_reasons": [],
"should_show_consent_popup": false,
"app_integration": {
"id": "10897",
"type": "app_integration"
}
"displayName": "Google Docs",
"displayDescription": "Open with Google Docs",
"displayOrder": 3,
"isDisabled": true,
"DisabledReasons": ["manually disabled"],
"requiresConsent": false,
"appIntegrationId": "10897",
"type": "app_integration"
}
],
"boxEditAndSFC": [
{
"displayName": "Open",
"displayDescription": "Open this file on your desktop",
"displayOrder": 1,
"isDisabled": false,
"requiresConsent": false,
"DisabledReasons": [],
"appIntegrationId": "13418",
"type": "app_integration"
},
{
"displayName": "Open",
"displayDescription": "Open this file on your desktop",
"displayOrder": 2,
"isDisabled": false,
"requiresConsent": false,
"DisabledReasons": [],
"appIntegrationId": "1338",
"type": "app_integration"
},
{
"displayName": "Google Docs",
"displayDescription": "Open with Google Docs",
"displayOrder": 3,
"isDisabled": true,
"DisabledReasons": ["manually disabled"],
"requiresConsent": false,
"appIntegrationId": "10897",
"type": "app_integration"
}
]
}
}
38 changes: 33 additions & 5 deletions src/api/__tests__/OpenWith-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,50 @@ describe('api/ContentOpenWith', () => {

describe('getOpenWithIntegrations()', () => {
test('should format data on success', () => {
const data = mockOpenWithData.default;
const successFn = jest.fn();
const errorFn = jest.fn();
openWith.get = ({ successCallback }) => {
successCallback(mockOpenWithData);
successCallback(data);
};

openWith.formatOpenWithData = jest.fn();
openWith.consolidateBoxEditIntegrations = jest.fn();

openWith.getOpenWithIntegrations('123', successFn, errorFn);
expect(openWith.formatOpenWithData).toBeCalledWith(mockOpenWithData);
expect(openWith.formatOpenWithData).toBeCalledWith(data);
expect(openWith.consolidateBoxEditIntegrations).toBeCalled();
});
});

describe('consolidateBoxEditIntegrations()', () => {
test('should do nothing if no Box Edit integrations are present', () => {
const data = mockOpenWithData.default.items;
const result = openWith.consolidateBoxEditIntegrations(data);
expect(result).toEqual(data);
});

test('should do nothing if only Box Edit is present', () => {
const data = mockOpenWithData.boxEdit;
const result = openWith.consolidateBoxEditIntegrations(data);
expect(result).toEqual(data);
});
test('should do nothing if only Box Edit SFC is present', () => {
const data = mockOpenWithData.boxEditSFC;
const result = openWith.consolidateBoxEditIntegrations(data);
expect(result).toEqual(data);
});
test('should only return Box Edit if both Box Edit AND Box Edit SFC are present', () => {
const data = mockOpenWithData.boxEditAndSFC;
const result = openWith.consolidateBoxEditIntegrations(data);
expect(result).toEqual(mockOpenWithData.boxEdit);
});
});

describe('formatOpenWithData()', () => {
const data = mockOpenWithData.default;
test('should add a flattened and complete Integration', () => {
const formatedOpenWithIntegrations = openWith.formatOpenWithData(mockOpenWithData);
const formatedOpenWithIntegrations = openWith.formatOpenWithData(data);
formatedOpenWithIntegrations.forEach(integration => {
expect(typeof integration.appIntegrationId).toBe('string');
expect(typeof integration.displayDescription).toBe('string');
Expand All @@ -39,14 +67,14 @@ describe('api/ContentOpenWith', () => {
});

test('should add isDefault to all items', () => {
const formatedOpenWithIntegrations = openWith.formatOpenWithData(mockOpenWithData);
const formatedOpenWithIntegrations = openWith.formatOpenWithData(data);
formatedOpenWithIntegrations.forEach(integration => {
expect(typeof integration.isDefault).toBe('boolean');
});
});

test('should return items sorted by displayOrder', () => {
const formatedOpenWithIntegrations = openWith.formatOpenWithData(mockOpenWithData);
const formatedOpenWithIntegrations = openWith.formatOpenWithData(data);
formatedOpenWithIntegrations.forEach((integration, idx) => {
// displayOrder is 1 indexed
const expectedOrder = idx + 1;
Expand Down
Loading

0 comments on commit f19158d

Please sign in to comment.