| Conditions | 1 |
| Paths | 1 |
| Total Lines | 121 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | import chai from'chai' |
||
| 17 | describe('image', function() { |
||
| 18 | |||
| 19 | var pathToImage = path.join(config.root, config.publish.url, config.upload.image, 'chat-1.jpg'); |
||
|
|
|||
| 20 | var imagesListInFolder = [ |
||
| 21 | path.join(config.root, config.publish.url, config.upload.image, 'img.jpg'), |
||
| 22 | path.join(config.root, config.publish.url, config.upload.image, 'img_100x100.jpg'), |
||
| 23 | path.join(config.root, config.publish.url, config.upload.image, 'img_10x10.jpg'), |
||
| 24 | path.join(config.root, config.publish.url, config.upload.image, 'img_thumb.jpg') |
||
| 25 | ]; |
||
| 26 | |||
| 27 | before(function(done) { |
||
| 28 | fse.copySync(path.join(config.root, 'media'), config.root) |
||
| 29 | done() |
||
| 30 | }); |
||
| 31 | |||
| 32 | /** |
||
| 33 | * cmsMedia.image.generateThumbnail |
||
| 34 | * @todo : UNIT TEST this method |
||
| 35 | * |
||
| 36 | */ |
||
| 37 | it('cmsMedia.image.generateThumbnail()', function(done) { |
||
| 38 | // this.sinon = sinon.sandbox.create(); |
||
| 39 | // var stub = sinon.stub(cmsMedia.image, 'smartCropAndSaveFile') |
||
| 40 | // stub.returns(Promise.resolve(0)) |
||
| 41 | // var thumb = cmsMedia.image.generateThumbnail(pathToImage) |
||
| 42 | // thumb.then(function (result) { |
||
| 43 | // done() |
||
| 44 | // chai.expect(result).to.not.be.undefined |
||
| 45 | // chai.expect(result.thumb).to.not.be.undefined |
||
| 46 | // chai.expect(result.thumb).to.equal('/unitimage/chat-1_thumb.jpg') |
||
| 47 | // sinon.assert.calledOnce(cmsMedia.image.smartCropAndSaveFile) |
||
| 48 | // cmsMedia.image.smartCropAndSaveFile.restore() |
||
| 49 | // }) |
||
| 50 | done() |
||
| 51 | }); |
||
| 52 | |||
| 53 | /** |
||
| 54 | * cmsMedia.image.cropAndSaveFiles |
||
| 55 | * @todo : UNIT TEST this method |
||
| 56 | * |
||
| 57 | */ |
||
| 58 | it('cmsMedia.image.cropAndSaveFiles()', function(done) { |
||
| 59 | // this.sinon = sinon.sandbox.create(); |
||
| 60 | // var stub = sinon.stub(cmsMedia.image, 'smartCropAndSaveFile') |
||
| 61 | // stub.returns(Promise.resolve({ |
||
| 62 | // 'stdout': false, |
||
| 63 | // 'stderr': false |
||
| 64 | // })) |
||
| 65 | // var thumbs = cmsMedia.image.cropAndSaveFiles(['10x10'], pathToImage, {}) |
||
| 66 | |||
| 67 | // thumbs.then(function (result) { |
||
| 68 | // console.log("result", result) |
||
| 69 | // chai.expect(result).to.not.be.undefined |
||
| 70 | // chai.expect(result.thumbs).to.have.length(1) |
||
| 71 | // chai.expect(result.thumbs[0]).to.have.property('name').to.equal('/unitimage/chat-1_10x10.jpg') |
||
| 72 | // chai.expect(result.thumbs[0]).to.have.property('size').to.equal('10x10') |
||
| 73 | // sinon.assert.calledOnce(cmsMedia.image.smartCropAndSaveFile) |
||
| 74 | // cmsMedia.image.smartCropAndSaveFile.restore() |
||
| 75 | |||
| 76 | // done() |
||
| 77 | // }) |
||
| 78 | // .catch(function (err) { |
||
| 79 | // console.log("catch", err) |
||
| 80 | // done() |
||
| 81 | // }) |
||
| 82 | done() |
||
| 83 | }); |
||
| 84 | |||
| 85 | /** |
||
| 86 | * cmsMedia.image.getThumbsList |
||
| 87 | * |
||
| 88 | */ |
||
| 89 | it('cmsMedia.image.getThumbsList()', function() { |
||
| 90 | this.sinon = sinon.sandbox.create(); |
||
| 91 | var stub = sinon.stub(coreUtils.file, 'getFilesSync') |
||
| 92 | stub.returns(imagesListInFolder) |
||
| 93 | var result = cmsMedia.image.getThumbsList() |
||
| 94 | chai.expect(result).to.not.be.undefined |
||
| 95 | chai.expect(result).to.have.length(1) |
||
| 96 | chai.expect(result[0]).to.have.property('originalFile').to.equal('/unitimage/img.jpg') |
||
| 97 | chai.expect(result[0]).to.have.property('thumbFile').to.equal('/unitimage/img_thumb.jpg') |
||
| 98 | sinon.assert.calledOnce(coreUtils.file.getFilesSync) |
||
| 99 | coreUtils.file.getFilesSync.restore() |
||
| 100 | }); |
||
| 101 | |||
| 102 | /** |
||
| 103 | * cmsMedia.image.getAssociatedImageFileFromThumb |
||
| 104 | * |
||
| 105 | */ |
||
| 106 | it('cmsMedia.image.getAssociatedImageFileFromThumb()', function() { |
||
| 107 | this.sinon = sinon.sandbox.create(); |
||
| 108 | var stub = sinon.stub(coreUtils.file, 'getFilesSync') |
||
| 109 | stub.returns(imagesListInFolder) |
||
| 110 | var result = cmsMedia.image.getAssociatedImageFileFromThumb('/unitimage/img_thumb.jpg') |
||
| 111 | chai.expect(result).to.not.be.undefined |
||
| 112 | chai.expect(result).to.have.property('originalFile').to.equal('/unitimage/img.jpg') |
||
| 113 | chai.expect(result).to.have.property('thumbFile').to.equal('/unitimage/img_thumb.jpg') |
||
| 114 | chai.expect(result.thumbs).to.have.length(2) |
||
| 115 | chai.expect(result.thumbs[0]).to.equal('/unitimage/img_100x100.jpg') |
||
| 116 | sinon.assert.calledOnce(coreUtils.file.getFilesSync) |
||
| 117 | coreUtils.file.getFilesSync.restore() |
||
| 118 | }); |
||
| 119 | |||
| 120 | /** |
||
| 121 | * cmsMedia.image.isValidMedia |
||
| 122 | * |
||
| 123 | */ |
||
| 124 | it('cmsMedia.image.isValidMedia()', function() { |
||
| 125 | var result = cmsMedia.image.isValidMedia('image/jpeg', '.jpg') |
||
| 126 | var result2 = cmsMedia.image.isValidMedia('wrong/mimetype', '.exe') |
||
| 127 | chai.expect(result.error).to.equal(false) |
||
| 128 | chai.expect(result2.error).to.be.a('string') |
||
| 129 | chai.expect(result2.error).to.equal('unauthorized file') |
||
| 130 | }); |
||
| 131 | |||
| 132 | after(function(done) { |
||
| 133 | fse.remove(path.join(config.root, config.publish.url)) |
||
| 134 | done() |
||
| 135 | }); |
||
| 136 | |||
| 137 | }); |
||
| 138 |