Skip to content

Commit

Permalink
features and demo
Browse files Browse the repository at this point in the history
  • Loading branch information
Maya Shavin committed Nov 3, 2020
1 parent e04f03e commit 03d2889
Show file tree
Hide file tree
Showing 71 changed files with 3,183 additions and 402 deletions.
13 changes: 12 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,18 @@
"pretest:ci": "yarn pretest",
"test:ci": "lerna run test:ci --stream",
"prettier": "yarn prettier --write .",
"build:demo": "npm --prefix packages/demo run build"
"build:demo": "npm --prefix packages/demo run build",
"changelog": "node scripts/changelog.js",
"dev:demo": "npm --prefix packages/demo run develop",
"explore:demo": "npm --prefix packages/demo run explore",
"dev:transformer": "node packages/gs-cld-transformer/example.js",
"demo": "yarn workspace demo",
"api": "yarn workspace api",
"test:api": "npm --prefix packages/api run test:unit",
"build:api": "npm --prefix packages/api run build",
"build:transformer": "npm --prefix packages/gs-cld-transformer run build",
"transformer": "yarn build:api && yarn build:transformer && yarn dev:demo",
"source": "yarn build:api && yarn dev:demo"
},
"repository": {
"type": "git",
Expand Down
75 changes: 75 additions & 0 deletions packages/api/__tests__/actions.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import { resize, rotate, roundCorners, quality, format, border } from '../lib/actions'

describe('Actions', () => {
describe('resize()', () => {
it('should return with all options', () => {
expect(resize({type: 'crop', width: 10, height: 20})).toEqual('c_crop,w_10,h_20')
})

it('should return only type and height', () => {
expect(resize({ type: 'crop', height: 20 })).toEqual('c_crop,h_20')
})

it('should return only type and width', () => {
expect(resize({ type: 'crop', width: '10' })).toEqual('c_crop,w_10')
})

it('should auto-scale when only width and height', () => {
expect(resize({ width:10, height: 10 })).toEqual('c_scale,w_10,h_10')
})
})

describe('rotate', () => {
it('should return rotation with angel', () => {
expect(rotate(10)).toEqual('a_10')
})

it('should return empty', () => {
expect(rotate(0)).toEqual('')
})
})

describe('roundCorners()', () => {
it('should return valid option', () => {
expect(roundCorners('max')).toEqual('r_max')
})
})

describe('quality()', () => {
it('should return default', () => {
expect(quality()).toEqual('q_auto')
});

it('should return default on undefined', () => {
expect(quality(undefined)).toEqual('q_auto')
})

it('should return valid option', () => {
expect(quality(300)).toEqual('q_300')
});
});

describe('format()', () => {
it('should return default', () => {
expect(format()).toEqual('f_auto')
});

it('should return default on undefined', () => {
expect(format(undefined)).toEqual('f_auto')
})

it('should return valid option', () => {
expect(format('webp')).toEqual('f_webp')
});
});

describe('border', () => {
it('should return a default type and color if only width is passed', () => {
expect(border({ width: 10 })).toEqual('bo_10px_solid_black')
})

it('should return options', () => {
expect(border({ type: 'dotted', color: 'blue', width: 10 })).toEqual('bo_10px_dotted_blue')
})
})
})
7 changes: 0 additions & 7 deletions packages/api/__tests__/api.test.js

This file was deleted.

75 changes: 75 additions & 0 deletions packages/api/__tests__/cImage.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import CImage from '../lib/cImage'
import { Actions } from '../lib/actions'

describe('CImage', () => {
it('should contains only the actions passed', () => {
const cimg = new CImage('a', [ Actions.resize ], {
cloud: {
cloudName: 'demo'
},
url: {
secure: true
}
})

expect(cimg.resize).toBeDefined()
expect(cimg.rotate).toBeUndefined()
});

it('should contains more than two actions passed', () => {
const cimg = new CImage('a', [ Actions.resize, Actions.rotate ], {
cloud: {
cloudName: 'demo'
},
url: {
secure: true
}
})

expect(cimg.resize).toBeDefined()
expect(cimg.rotate).toBeDefined()
});

it('should have action triggerable', () => {
const cimg = new CImage('a', [ Actions.quality ], {
cloud: {
cloudName: 'demo'
},
url: {
secure: true
}
})

cimg.quality()

expect(cimg.actions).toEqual(['f_auto','q_auto'])
})

it('should have valid url', () => {
const cimg = new CImage('a', [ Actions.resize ], {
cloud: {
cloudName: 'demo'
},
url: {
secure: true
}
})

cimg.resize({ width: 100, height: 100 })

expect(cimg.url()).toEqual('https://res.cloudinary.com/demo/image/upload/f_auto/q_auto/c_scale,w_100,h_100/a')
})

it('should have valid url with basic transformation', () => {
const cimg = new CImage('a', [ Actions.rotate, Actions.resize ], {
cloud: {
cloudName: 'demo'
},
url: {
secure: true
}
})

expect(cimg.url()).toEqual('https://res.cloudinary.com/demo/image/upload/f_auto/q_auto/a')
})
});
124 changes: 124 additions & 0 deletions packages/api/__tests__/cloudinary.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
import CloudinaryApi from '../lib/cloudinary'

describe('CloudinaryApi', () => {
const mockReporterError = jest.fn()
jest.mock('../lib/utils/reporter', () => ({
error: mockReporterError
}))

it('should throw error when no cloudName defined ', () => {
const options = {
use: {
image: []
},
components: {}
}

new CloudinaryApi(options)

expect(mockReporterError).toBeCalled()
})

it('should throw error when no image or video selected', () => {
const options = {
cloudName: 'demo',
use: {},
components: {}
}

const instance = new CloudinaryApi(options)

expect(mockReporterError).toBeCalledTimes(1)
expect(instance.image).toBeUndefined()
expect(instance.video).toBeUndefined()
})

it('should create instance of Cloudinary with options', () => {
const options = {
cloudName: 'demo',
apiKey: '123',
api_Secret: '1234',
private: false,
privateCdn: true,
secure: true,
use: {
image: ['rotate', 'resize']
}
}

const instance = new CloudinaryApi(options)

expect(mockReporterError).not.toBeCalled()
expect(instance.cloudConfig).toEqual({
cloudName: 'demo',
apiKey: '123'
})
expect(instance.urlConfig).toEqual({
secure: true,
privateCdn: true,
})
expect(instance.image).toBeDefined()
expect(instance.video).toBeUndefined()
})

it('should create instance of Cloudinary with only image features', () => {
const options = {
cloudName: 'demo',
apiKey: '123',
api_Secret: '1234',
private: false,
privateCdn: true,
secure: true,
use: {
image: ['rotate', 'resize']
}
}

const instance = new CloudinaryApi(options)

expect(mockReporterError).not.toBeCalled
expect(instance.cloudConfig).toEqual({
cloudName: 'demo',
apiKey: '123'
})
expect(instance.urlConfig).toEqual({
secure: true,
privateCdn: true,
})
expect(instance.image('sample').rotate).toBeDefined()
expect(instance.image('sample').resize).toBeDefined()
expect(instance.image('sample').quality).toBeUndefined()
})

it('should create instance of Cloudinary with only video features', () => {
const options = {
cloudName: 'demo',
apiKey: '123',
api_Secret: '1234',
private: false,
privateCdn: true,
secure: true,
use: {
video: ['rotate', 'resize']
}
}

const instance = new CloudinaryApi(options)

expect(mockReporterError).not.toBeCalled()
expect(instance.cloudConfig).toEqual({
cloudName: 'demo',
apiKey: '123'
})
expect(instance.urlConfig).toEqual({
secure: true,
privateCdn: true,
})
expect(instance.image).toBeUndefined()
expect(instance.video).toBeDefined()
})

afterEach(() => {
jest.clearAllMocks()
})
});
57 changes: 57 additions & 0 deletions packages/api/__tests__/url.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import Url from '../lib/utils/url'

describe('Url', () => {
describe('getPrefix()', () => {
it('should return a prefix with image as default', () => {
expect(Url.getPrefix({
cloudName: 'demo',
resourceType: 'upload'
})).toEqual('https://res.cloudinary.com/demo/image/upload')
})

it('should return a prefix', () => {
expect(Url.getPrefix({
cloudName: 'demo',
resourceType: 'upload',
assetType: 'video'
})).toEqual('https://res.cloudinary.com/demo/video/upload')
})
})

describe('getPathToAsset', () => {
it('should force the default version', () => {
expect(Url.getPathToAsset({
publicId: 'a',
forceVersion: true
})).toEqual('v1/a')
});

it('should force the version when passed', () => {
expect(Url.getPathToAsset({
publicId: 'a',
forceVersion: true,
version: 2
})).toEqual('v2/a')
});

it('should not force version when not enabled ', () => {
expect(Url.getPathToAsset({
publicId: 'a',
})).toEqual('a')
});

it('should not force a version when it is a url ', () => {
expect(Url.getPathToAsset({
forceVersion: true,
publicId: 'https://hello'
})).toEqual('https://hello')
});

it('should not force a version when it already contains a version ', () => {
expect(Url.getPathToAsset({
forceVersion: true,
publicId: 'v3/a'
})).toEqual('v3/a')
});
})
})
Loading

0 comments on commit 03d2889

Please sign in to comment.