Path: blob/master/node_modules/@jimp/plugin-contain/test/contain.test.js
1129 views
import { Jimp, mkJGD, hasOwnProp } from '@jimp/test-utils';1import configure from '@jimp/custom';2import blit from '@jimp/plugin-blit';3import resize from '@jimp/plugin-resize';4import scale from '@jimp/plugin-scale';56import contain from '../src';78const jimp = configure({ plugins: [scale, resize, blit, contain] }, Jimp);910describe('All align combinations for contain', () => {11const verticalJGD = mkJGD(12'▴▴▴▴▸▸▸▸',13'▴▴▴▴▸▸▸▸',14'▴▴▴▴▸▸▸▸',15'▴▴▴▴▸▸▸▸',16'▴▴▴▴▸▸▸▸',17'▴▴▴▴▸▸▸▸',18'▾▾▾▾◆◆◆◆',19'▾▾▾▾◆◆◆◆',20'▾▾▾▾◆◆◆◆',21'▾▾▾▾◆◆◆◆',22'▾▾▾▾◆◆◆◆',23'▾▾▾▾◆◆◆◆'24);2526const horizontalJGD = mkJGD(27'▴▴▴▴▴▴▸▸▸▸▸▸',28'▴▴▴▴▴▴▸▸▸▸▸▸',29'▴▴▴▴▴▴▸▸▸▸▸▸',30'▴▴▴▴▴▴▸▸▸▸▸▸',31'▾▾▾▾▾▾◆◆◆◆◆◆',32'▾▾▾▾▾▾◆◆◆◆◆◆',33'▾▾▾▾▾▾◆◆◆◆◆◆',34'▾▾▾▾▾▾◆◆◆◆◆◆'35);3637let vertical;38let horizontal; // stores the Jimp instances of the JGD images above.3940before(done => {41const img1 = jimp.read(verticalJGD);42const img2 = jimp.read(horizontalJGD);43Promise.all([img1, img2])44.then(images => {45vertical = images[0];46horizontal = images[1];47done();48})49.catch(done);50});5152const tests = {}; // Stores the expected result for each alignment combination.53tests['LEFT TOP'] = {54contain: {55verti: mkJGD('▴▴▸▸ ', '▴▴▸▸ ', '▴▴▸▸ ', '▾▾◆◆ ', '▾▾◆◆ ', '▾▾◆◆ '),56horiz: mkJGD('▴▴▴▸▸▸', '▴▴▴▸▸▸', '▾▾▾◆◆◆', '▾▾▾◆◆◆', ' ', ' ')57}58};59tests['CENTER TOP'] = {60contain: {61verti: mkJGD(' ▴▴▸▸ ', ' ▴▴▸▸ ', ' ▴▴▸▸ ', ' ▾▾◆◆ ', ' ▾▾◆◆ ', ' ▾▾◆◆ '),62horiz: mkJGD('▴▴▴▸▸▸', '▴▴▴▸▸▸', '▾▾▾◆◆◆', '▾▾▾◆◆◆', ' ', ' ')63}64};65tests['RIGHT TOP'] = {66contain: {67verti: mkJGD(' ▴▴▸▸', ' ▴▴▸▸', ' ▴▴▸▸', ' ▾▾◆◆', ' ▾▾◆◆', ' ▾▾◆◆'),68horiz: mkJGD('▴▴▴▸▸▸', '▴▴▴▸▸▸', '▾▾▾◆◆◆', '▾▾▾◆◆◆', ' ', ' ')69}70};7172tests['LEFT MIDDLE'] = {73contain: {74verti: mkJGD('▴▴▸▸ ', '▴▴▸▸ ', '▴▴▸▸ ', '▾▾◆◆ ', '▾▾◆◆ ', '▾▾◆◆ '),75horiz: mkJGD(' ', '▴▴▴▸▸▸', '▴▴▴▸▸▸', '▾▾▾◆◆◆', '▾▾▾◆◆◆', ' ')76}77};78tests['CENTER MIDDLE'] = {79contain: {80verti: mkJGD(' ▴▴▸▸ ', ' ▴▴▸▸ ', ' ▴▴▸▸ ', ' ▾▾◆◆ ', ' ▾▾◆◆ ', ' ▾▾◆◆ '),81horiz: mkJGD(' ', '▴▴▴▸▸▸', '▴▴▴▸▸▸', '▾▾▾◆◆◆', '▾▾▾◆◆◆', ' ')82}83};84tests['RIGHT MIDDLE'] = {85contain: {86verti: mkJGD(' ▴▴▸▸', ' ▴▴▸▸', ' ▴▴▸▸', ' ▾▾◆◆', ' ▾▾◆◆', ' ▾▾◆◆'),87horiz: mkJGD(' ', '▴▴▴▸▸▸', '▴▴▴▸▸▸', '▾▾▾◆◆◆', '▾▾▾◆◆◆', ' ')88}89};9091tests['LEFT BOTTOM'] = {92contain: {93verti: mkJGD('▴▴▸▸ ', '▴▴▸▸ ', '▴▴▸▸ ', '▾▾◆◆ ', '▾▾◆◆ ', '▾▾◆◆ '),94horiz: mkJGD(' ', ' ', '▴▴▴▸▸▸', '▴▴▴▸▸▸', '▾▾▾◆◆◆', '▾▾▾◆◆◆')95}96};97tests['CENTER BOTTOM'] = {98contain: {99verti: mkJGD(' ▴▴▸▸ ', ' ▴▴▸▸ ', ' ▴▴▸▸ ', ' ▾▾◆◆ ', ' ▾▾◆◆ ', ' ▾▾◆◆ '),100horiz: mkJGD(' ', ' ', '▴▴▴▸▸▸', '▴▴▴▸▸▸', '▾▾▾◆◆◆', '▾▾▾◆◆◆')101}102};103tests['RIGHT BOTTOM'] = {104contain: {105verti: mkJGD(' ▴▴▸▸', ' ▴▴▸▸', ' ▴▴▸▸', ' ▾▾◆◆', ' ▾▾◆◆', ' ▾▾◆◆'),106horiz: mkJGD(' ', ' ', '▴▴▴▸▸▸', '▴▴▴▸▸▸', '▾▾▾◆◆◆', '▾▾▾◆◆◆')107}108};109110function runAlignTest(align) {111const jgdContainV = tests[align].contain.verti;112const jgdContainH = tests[align].contain.horiz;113let a = align.split(' ');114a = Jimp['HORIZONTAL_ALIGN_' + a[0]] | Jimp['VERTICAL_ALIGN_' + a[1]];115it('contain aligned to ' + align, () => {116vertical117.clone()118.contain(6, 6, a)119.getJGDSync()120.should.be.sameJGD(jgdContainV, 'Vertical image');121horizontal122.clone()123.contain(6, 6, a)124.getJGDSync()125.should.be.sameJGD(jgdContainH, 'Horizontal image');126});127}128129for (const align in tests) if (hasOwnProp(tests, align)) runAlignTest(align);130});131132133