Skip to content

Commit

Permalink
Moved dedent helper from RelayTestUtils.js to dedent.js
Browse files Browse the repository at this point in the history
Reviewed By: wincent

Differential Revision: D3508229

fbshipit-source-id: d7986d86d09cc7ff18e5b9bf33d540a5f366ae77
  • Loading branch information
sampepose authored and Facebook Github Bot 4 committed Jul 1, 2016
1 parent 7a32f80 commit 2e49c95
Show file tree
Hide file tree
Showing 4 changed files with 211 additions and 182 deletions.
38 changes: 0 additions & 38 deletions src/tools/__mocks__/RelayTestUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -140,44 +140,6 @@ const RelayTestUtils = {
);
},

/**
* Strips leading indentation from a multi-line string, enabling you to use
* template literals in tests while maintaining nice nesting.
*
* An optional padding string can be supplied to force a fixed indent to be
* applied back after stripping.
*/
dedent(string, padding = '') {
const blankLine = /^\s*$/;
const lines = string.split('\n');

// Remove any entirely blank leading or trailing lines.
if (lines.length && lines[0].match(blankLine)) {
lines.shift();
}
if (lines.length && lines[lines.length - 1].match(blankLine)) {
lines.pop();
}

const minLeadingSpace = lines.reduce((acc, line) => {
if (line.match(blankLine)) {
return acc;
}
const leadingWhitespace = line.match(/^(\s*)/);
return Math.min(
acc,
leadingWhitespace ? leadingWhitespace[1].length : 0,
);
}, Infinity);

return lines.map(line => {
if (line.match(blankLine)) {
return '';
}
return padding + line.slice(minLeadingSpace);
}).join('\n');
},

defer(fragment) {
const QueryBuilder = require('QueryBuilder');
const RelayFragmentReference = require('RelayFragmentReference');
Expand Down
144 changes: 0 additions & 144 deletions src/tools/__tests__/RelayTestUtils-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,150 +14,6 @@
const RelayTestUtils = require('RelayTestUtils');

describe('RelayTestUtils', () => {
describe('dedent()', () => {
const {dedent} = RelayTestUtils;

describe('with an empty string', () => {
it('does nothing', () => {
expect(dedent('')).toBe('');
});

it('does not pad a blank line', () => {
expect(dedent('', ' ')).toBe('');
});
});

describe('with a blank string', () => {
it('strips the entire string', () => {
expect(dedent(' ')).toBe('');
});

it('does not pad a blank line', () => {
expect(dedent(' ', ' ')).toBe('');
});
});

describe('with a simple string', () => {
it('does nothing when there is no indent', () => {
expect(dedent('foo')).toBe('foo');
});

it('strips a leading indent', () => {
expect(dedent(' foo')).toBe('foo');
});

it('applies a custom padding if requested', () => {
expect(dedent(' foo', ' ')).toBe(' foo');
});

it('does nothing to trailing whitespace', () => {
expect(dedent('foo ')).toBe('foo ');
expect(dedent('foo ', ' ')).toBe(' foo ');
});
});

describe('with a multiline string', () => {
it('dedents based on the leading whitespace', () => {
const string = `
query MyQuery {
example {
text
}
}
`;
expect(dedent(string)).toBe(
'query MyQuery {\n' +
' example {\n' +
' text\n' +
' }\n' +
'}'
);
});

it('applies a custom padding if requested', () => {
const string = `
query MyQuery {
example {
text
}
}
`;
expect(dedent(string, ' ')).toBe(
' query MyQuery {\n' +
' example {\n' +
' text\n' +
' }\n' +
' }'
);
});

it('is deals with zero-width lines', () => {
const string = `
query MyQuery {
example {
text
}
more
}
`;
expect(dedent(string)).toBe(
'query MyQuery {\n' +
' example {\n' +
' text\n' +
' }\n' +
'\n' +
' more\n' +
'}'
);
});

it('does not insert entirely blank lines when applying padding', () => {
const string = `
query MyQuery {
example {
text
}
more
}
`;
expect(dedent(string, ' ')).toBe(
' query MyQuery {\n' +
' example {\n' +
' text\n' +
' }\n' +
'\n' +
' more\n' +
' }'
);
});

it('suppresses blank lines in the output', () => {
/* eslint-disable no-trailing-spaces */
const string = `
query MyQuery {
example {
text
}
more
}
`;
/* eslint-enable no-trailing-spaces */
expect(dedent(string, ' ')).toBe(
' query MyQuery {\n' +
' example {\n' +
' text\n' +
' }\n' +
'\n' +
' more\n' +
' }'
);
});
});
});

describe('matchers', () => {
let comparator;

Expand Down
158 changes: 158 additions & 0 deletions src/tools/__tests__/dedent-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
/**
* Copyright (c) 2013-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*
* @emails oncall+relay
*/

'use strict';

jest.unmock('dedent');

const dedent = require('dedent');

describe('dedent()', () => {
describe('with an empty string', () => {
it('does nothing', () => {
expect(dedent('')).toBe('');
});

it('does not pad a blank line', () => {
expect(dedent('', ' ')).toBe('');
});
});

describe('with a blank string', () => {
it('strips the entire string', () => {
expect(dedent(' ')).toBe('');
});

it('does not pad a blank line', () => {
expect(dedent(' ', ' ')).toBe('');
});
});

describe('with a simple string', () => {
it('does nothing when there is no indent', () => {
expect(dedent('foo')).toBe('foo');
});

it('strips a leading indent', () => {
expect(dedent(' foo')).toBe('foo');
});

it('applies a custom padding if requested', () => {
expect(dedent(' foo', ' ')).toBe(' foo');
});

it('does nothing to trailing whitespace', () => {
expect(dedent('foo ')).toBe('foo ');
expect(dedent('foo ', ' ')).toBe(' foo ');
});
});

describe('with a multiline string', () => {
it('dedents based on the leading whitespace', () => {
const string = `
query MyQuery {
example {
text
}
}
`;
expect(dedent(string)).toBe(
'query MyQuery {\n' +
' example {\n' +
' text\n' +
' }\n' +
'}'
);
});

it('applies a custom padding if requested', () => {
const string = `
query MyQuery {
example {
text
}
}
`;
expect(dedent(string, ' ')).toBe(
' query MyQuery {\n' +
' example {\n' +
' text\n' +
' }\n' +
' }'
);
});

it('is deals with zero-width lines', () => {
const string = `
query MyQuery {
example {
text
}
more
}
`;
expect(dedent(string)).toBe(
'query MyQuery {\n' +
' example {\n' +
' text\n' +
' }\n' +
'\n' +
' more\n' +
'}'
);
});

it('does not insert entirely blank lines when applying padding', () => {
const string = `
query MyQuery {
example {
text
}
more
}
`;
expect(dedent(string, ' ')).toBe(
' query MyQuery {\n' +
' example {\n' +
' text\n' +
' }\n' +
'\n' +
' more\n' +
' }'
);
});

it('suppresses blank lines in the output', () => {
/* eslint-disable no-trailing-spaces */
const string = `
query MyQuery {
example {
text
}
more
}
`;
/* eslint-enable no-trailing-spaces */
expect(dedent(string, ' ')).toBe(
' query MyQuery {\n' +
' example {\n' +
' text\n' +
' }\n' +
'\n' +
' more\n' +
' }'
);
});
});
});
Loading

0 comments on commit 2e49c95

Please sign in to comment.