-
Notifications
You must be signed in to change notification settings - Fork 97
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
introduce upload entity and related tests
- Loading branch information
Benedikt Rötsch
committed
Feb 28, 2017
1 parent
c3cb996
commit a4805b4
Showing
6 changed files
with
184 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import cloneDeep from 'lodash/cloneDeep' | ||
import { freezeSys, toPlainObject } from 'contentful-sdk-core' | ||
import enhanceWithMethods from '../enhance-with-methods' | ||
import { | ||
createDeleteEntity | ||
} from '../instance-actions' | ||
|
||
/** | ||
* @typedef {Upload} Upload | ||
* @property {Object} sys - Standard system metadata with additional asset specific properties | ||
* @property {string} sys.id - The id of the upload | ||
* @property {function(): Promise<Asset>} delete - Deletes an upload | ||
* @property {function(): Object} toPlainObject - Returns this Asset as a plain JS object | ||
* @example | ||
* | ||
* // require contentful-management | ||
* var contentfulManagement = require('contentful-management') | ||
* var client = contentfulManagement.createClient({ | ||
* // This is the access token for this space. Normally you get both ID and the token in the Contentful web app | ||
* accessToken: 'YOUR_ACCESS_TOKEN' | ||
* }) | ||
* | ||
* var space = await client.getSpace('SPACE_ID') | ||
* const uploadStream = createReadStream('path/to/filename_english.jpg') | ||
* space.createUpload(uploadStream) | ||
* .then((upload) => { | ||
* // Delte upload again | ||
* return upload.delete() | ||
* }) | ||
* .catch(err => console.log(err)) | ||
*/ | ||
|
||
function createUploadApi (http) { | ||
return { | ||
/** | ||
* Deletes this object on the server. | ||
* @memberof Upload | ||
* @func delete | ||
* @return {Promise} Promise for the deletion. It contains no data, but the Promise error case should be handled. | ||
* @example | ||
*/ | ||
delete: createDeleteEntity({ | ||
http: http, | ||
entityPath: 'uploads' | ||
}) | ||
} | ||
} | ||
|
||
/** | ||
* @private | ||
* @param {Object} http - HTTP client instance | ||
* @param {Object} data - Raw upload data | ||
* @return {Asset} Wrapped upload data | ||
*/ | ||
export function wrapUpload (http, data) { | ||
const upload = toPlainObject(cloneDeep(data)) | ||
enhanceWithMethods(upload, createUploadApi(http)) | ||
return freezeSys(upload) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import test from 'tape' | ||
import { cloneMock } from '../mocks/entities' | ||
import setupHttpMock from '../mocks/http' | ||
import { wrapUpload } from '../../../lib/entities/upload' | ||
import { | ||
entityWrappedTest, | ||
entityDeleteTest, | ||
failingActionTest | ||
} from '../test-creators/instance-entity-methods' | ||
|
||
function setup (promise) { | ||
return { | ||
httpMock: setupHttpMock(promise), | ||
entityMock: cloneMock('upload') | ||
} | ||
} | ||
|
||
test('Upload is wrapped', (t) => { | ||
entityWrappedTest(t, setup, { | ||
wrapperMethod: wrapUpload | ||
}) | ||
}) | ||
|
||
test('Upload delete', (t) => { | ||
return entityDeleteTest(t, setup, { | ||
wrapperMethod: wrapUpload | ||
}) | ||
}) | ||
|
||
test('Upload delete fails', (t) => { | ||
return failingActionTest(t, setup, { | ||
wrapperMethod: wrapUpload, | ||
actionMethod: 'delete' | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters