Home Reference Source

application/__tests__/util/strings-test.js

jest.unmock('./../../util/strings')

import React from 'react';
import ReactDOM from 'react-dom';
import TestUtils from 'react-addons-test-utils';
import {sluggify, urlify, parameterize, timeToFormat, parseSrn, htmlDiff, degraf} from './../../util/strings';

describe('sluggify', () => {
    it('returns slugged string', () => {
        const str = sluggify(' this is a test  HELLO <splat> there 1  ');
        expect(str).toEqual('this-is-a-test-hello-splat-there-1');
    });

    it('returns trailing hyphen when asked', () => {
        const str = sluggify(' this is a test  HELLO <splat> there 1  ', true);
        expect(str).toEqual('this-is-a-test-hello-splat-there-1-');
    });
});

describe('urlify', () => {
    it('returns formatted url', () => {
        const str = '/this-is/a/test';
        expect(urlify(str)).toEqual('this-is/a/test');
    });

    it('returns formatted url with slashes', () => {
        const str = '/this-is/a/test/';
        expect(urlify(str, false)).toEqual('/this-is/a/test/');
    });

    it('deslashes', () => {
        const str = '//this-is/a//test/';
        expect(urlify(str, false)).toEqual('/this-is/a/test/');
    });

    it('deslashes both ends', () => {
        const str = '//this-is/a//test//';
        expect(urlify(str)).toEqual('this-is/a/test');
    });

});

describe('parameterize', () => {
    it('returns flat string', () => {
        const str = parameterize({'first': 'foo', 'second': 'bar'});
        expect(str).toEqual('first=foo&second=bar');
    });

    it('returns flat encoded entities', () => {
        const str = parameterize({'first': 'foo', 'second': 'this is a test!?'});
        expect(str).toEqual('first=foo&second=this%20is%20a%20test!%3F');
    });

    it('flatens children', () => {
        const str = parameterize({'filter': ['first', 'second', 'third']});

        expect(str).toEqual('filter%5B0%5D=first&filter%5B1%5D=second&filter%5B2%5D=third');
    });

    it('flatens keyed children', () => {
        const str = parameterize({'filter': {'first': 'one', 'second': 'two'}});

        expect(str).toEqual('filter%5Bfirst%5D=one&filter%5Bsecond%5D=two');
    });
});

describe('timeToFormat', () => {
    it('returns default time', () => {
        const check = '2016-03-22 18:35:30';
        const str = timeToFormat(check);
        expect(str).toEqual('3/22/2016 2:35 PM');
    });

    it('returns a formatted time', () => {
        const check = '2016-03-22 18:35:30'
        const str = timeToFormat(check, 'LTS');
        expect(str).toEqual('2:35:30 PM');
    });
});

describe('parseSrn', () => {
    it('returns proper srn', () => {
        const check = 'srn:tsn:ceo-core/content:920da794-c8bf-4d76-b970-4733f2f95fa5';
        const srn = parseSrn(check);

        expect(srn.client).toEqual('tsn');
        expect(srn.app).toEqual('ceo-core');
        expect(srn.model).toEqual('content');
        expect(srn.uuid).toEqual('920da794-c8bf-4d76-b970-4733f2f95fa5');
    });
});

describe('htmlDiff', () => {
    it('returns inserted data', () => {
        const strA = '<p>This is a test</p>';
        const strB = '<p>This is also a test</p>';

        expect(htmlDiff(strA, strB)).toEqual('<p>This is <ins>also </ins>a test</p>');
    });

    it('returns removed data', () => {
        const strA = '<p>This is a test</p>';
        const strB = '<p>This is also a test</p>';

        expect(htmlDiff(strB, strA)).toEqual('<p>This is <del>also </del>a test</p>');
    });

});

describe('degraf', () => {

    it('removes excess grafs', () => {
        const strA = `<p>That morning, when Gregor Samsa woke from troubled dreams, he found himself transformed in his bed into a horrible vermin. He lay on his armour-like back, and if he lifted his head a <strong>little</strong> he could see his brown belly, slightly domed and divided by arches into stiff sections. The bedding was hardly able to cover it and seemed ready to slide off any moment.</p>
<p><br/></p>
<div class="pull-quote">In Soviet Russia, quotes pull YOU <span class="credit"></span></div>
<p>&nbsp;</p>
<p><br></p>
<p>His many legs, pitifully thin compared with the size of the rest of him, waved about helplessly as he looked. "What's happened to me? " he thought. It wasn't a dream. His room, a proper human room although a little too small, lay peacefully between its four familiar walls.</p>
<p><br></p>
<p><ins>Then there's this. </ins>Gregor then turned to look out the window at the dull weather. Drops of rain could be heard hitting the pane, which made him feel quite sad. "How about if I sleep a little bit longer and forget all this nonsense", he thought, but that was something he was unable to do because he was used to sleeping on his right, and in his present state couldn't get into that position. However hard he threw himself onto his right, he always rolled back to where he was. He<del> must have tried it a hundred times,</del> shut his eyes so that he wouldn't have to look at the floundering legs, and only stopped when</p>
<p><br /></p>
<p>A collection of textile samples lay spread out on the table - Samsa was a travelling salesman - and above it there hung a picture that he had recently cut out of an illustrated magazine and housed in a nice, gilded frame. It showed a lady fitted out with a fur hat and fur boa who sat upright, raising a heavy fur muff that covered the whole of her lower arm towards the viewer.</p>
<p><br></p>`;

        const strB = `<p>That morning, when Gregor Samsa woke from troubled dreams, he found himself transformed in his bed into a horrible vermin. He lay on his armour-like back, and if he lifted his head a <strong>little</strong> he could see his brown belly, slightly domed and divided by arches into stiff sections. The bedding was hardly able to cover it and seemed ready to slide off any moment.</p>
<div class="pull-quote">In Soviet Russia, quotes pull YOU <span class="credit"></span></div>
<p>His many legs, pitifully thin compared with the size of the rest of him, waved about helplessly as he looked. "What's happened to me? " he thought. It wasn't a dream. His room, a proper human room although a little too small, lay peacefully between its four familiar walls.</p>
<p><ins>Then there's this. </ins>Gregor then turned to look out the window at the dull weather. Drops of rain could be heard hitting the pane, which made him feel quite sad. "How about if I sleep a little bit longer and forget all this nonsense", he thought, but that was something he was unable to do because he was used to sleeping on his right, and in his present state couldn't get into that position. However hard he threw himself onto his right, he always rolled back to where he was. He<del> must have tried it a hundred times,</del> shut his eyes so that he wouldn't have to look at the floundering legs, and only stopped when</p>
<p>A collection of textile samples lay spread out on the table - Samsa was a travelling salesman - and above it there hung a picture that he had recently cut out of an illustrated magazine and housed in a nice, gilded frame. It showed a lady fitted out with a fur hat and fur boa who sat upright, raising a heavy fur muff that covered the whole of her lower arm towards the viewer.</p>`;

        expect(degraf(strA)).toEqual(strB);

    });

});