123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302 |
- import {
- deepFreeze,
- descopeFeatureName,
- escapeDoubleQuotes,
- explodeSlug,
- extractMultilineFields,
- parseRepo,
- sortProjects,
- valueIsEqual,
- } from 'sentry/utils';
- describe('utils.valueIsEqual', function () {
- it('should return true when objects are deeply equal', function () {
- const isEqual = valueIsEqual(
- {
- username: 'foo',
- teams: ['bar', 'baz'],
- avatar: {
- avatarType: 'gravatar',
- avatarUuid: null,
- },
- },
- {
- username: 'foo',
- teams: ['bar', 'baz'],
- avatar: {
- avatarType: 'gravatar',
- avatarUuid: null,
- },
- },
- true
- );
- expect(isEqual).toBe(true);
- });
- it('should return false when objects are not deeply equal', function () {
- const isEqual = valueIsEqual(
- {
- username: 'foo',
- teams: ['bar', 'baz'],
- avatar: {
- avatarType: 'gravatar',
- avatarUuid: null,
- },
- },
- {
- username: 'foo',
- teams: ['bar', 'baz'],
- avatar: {
- avatarType: 'notGravatar',
- avatarUuid: null,
- },
- },
- true
- );
- expect(isEqual).toBe(false);
- });
- it('should return true when objects are shallowly equal', function () {
- const isEqual = valueIsEqual(
- {
- username: 'foo',
- team: 'bar',
- avatar: 'gravatar',
- },
- {
- username: 'foo',
- team: 'bar',
- avatar: 'gravatar',
- },
- false
- );
- expect(isEqual).toBe(true);
- });
- it('should return false when objects are not shallowly equal', function () {
- const isEqual = valueIsEqual(
- {
- username: 'foo',
- team: 'bar',
- avatar: 'gravatar',
- },
- {
- username: 'foo',
- team: 'bar',
- avatar: 'notGravatar',
- },
- false
- );
- expect(isEqual).toBe(false);
- });
- it('should not blow up when comparing null value to an object', function () {
- let isEqual = valueIsEqual(null, {username: 'foo'}, true);
- expect(isEqual).toBe(false);
- isEqual = valueIsEqual(
- {
- username: 'foo',
- teams: ['bar', 'baz'],
- avatar: null,
- },
- {
- username: 'foo',
- teams: ['bar', 'baz'],
- avatar: {
- avatarType: 'notGravatar',
- avatarUuid: null,
- },
- },
- true
- );
- expect(isEqual).toBe(false);
- });
- });
- describe('utils.extractMultilineFields', function () {
- it('should work for basic, simple values', function () {
- expect(extractMultilineFields('one\ntwo\nthree')).toEqual(['one', 'two', 'three']);
- });
- it('should return an empty array if only whitespace', function () {
- expect(extractMultilineFields(' \n \n\n\n \n')).toEqual([]);
- });
- it('should trim values and ignore empty lines', function () {
- expect(
- extractMultilineFields(
- `one
- two
- three
- four
- five`
- )
- ).toEqual(['one', 'two', 'three', 'four', 'five']);
- });
- });
- describe('utils.parseRepo', function () {
- it('should work for simple github url', function () {
- expect(parseRepo('github.com/example/example')).toEqual('example/example');
- });
- it('should work for full github url', function () {
- expect(parseRepo('https://github.com/example/example')).toEqual('example/example');
- });
- it('should work for trailing slash', function () {
- expect(parseRepo('https://github.com/example/example/')).toEqual('example/example');
- });
- it('should work for simple BitBucket url', function () {
- expect(parseRepo('bitbucket.org/example/example')).toEqual('example/example');
- });
- it('should work for full BitBucket url', function () {
- expect(parseRepo('https://bitbucket.org/example/example')).toEqual('example/example');
- });
- it('should work for trailing Bitbucket slash', function () {
- expect(parseRepo('https://bitbucket.org/example/example/')).toEqual(
- 'example/example'
- );
- });
- it('should work for repo only', function () {
- expect(parseRepo('example/example')).toEqual('example/example');
- });
- it('should parse repo from url with extra info', function () {
- expect(parseRepo('github.com/example/example/commits/adsadsa')).toEqual(
- 'example/example'
- );
- });
- it('should work for nothing passed', function () {
- expect(parseRepo()).toEqual();
- });
- });
- describe('utils.explodeSlug', function () {
- it('replaces slug special chars with whitespace', function () {
- expect(explodeSlug('test--slug__replace-')).toEqual('test slug replace');
- });
- });
- describe('utils.projectDisplayCompare', function () {
- it('sorts by bookmark and project slug', function () {
- const projects = [
- {
- isBookmarked: true,
- slug: 'm',
- },
- {
- isBookmarked: false,
- slug: 'm',
- },
- {
- isBookmarked: false,
- slug: 'a',
- },
- {
- isBookmarked: true,
- slug: 'a',
- },
- {
- isBookmarked: true,
- slug: 'z',
- },
- {
- isBookmarked: false,
- slug: 'z',
- },
- ];
- const sortedProjects = sortProjects(projects);
- expect(sortedProjects).toEqual([
- {
- isBookmarked: true,
- slug: 'a',
- },
- {
- isBookmarked: true,
- slug: 'm',
- },
- {
- isBookmarked: true,
- slug: 'z',
- },
- {
- isBookmarked: false,
- slug: 'a',
- },
- {
- isBookmarked: false,
- slug: 'm',
- },
- {
- isBookmarked: false,
- slug: 'z',
- },
- ]);
- });
- });
- describe('utils.descopeFeatureName', function () {
- it('descopes the feature name', () => {
- [
- ['organizations:feature', 'feature'],
- ['projects:feature', 'feature'],
- ['unknown-scope:feature', 'unknown-scope:feature'],
- ['', ''],
- ].forEach(([input, expected]) => expect(descopeFeatureName(input)).toEqual(expected));
- });
- });
- describe('deepFreeze', function () {
- it('throws error on attempt to mutate frozen object', function () {
- const testObj = deepFreeze({foo: [1, 2, 3]});
- [
- () => {
- testObj.foo.push(4);
- },
- () => {
- testObj.bar = '';
- },
- () => {
- delete testObj.foo;
- },
- ].forEach(fn => {
- expect(fn).toThrow();
- });
- });
- });
- describe('utils.escapeDoubleQuotes', function () {
- // test cases from https://gist.github.com/getify/3667624
- it('should escape any unescaped double quotes', function () {
- const cases = [
- ['a"b', 'a\\"b'], //
- ['a\\"b', 'a\\"b'], //
- ['a\\\\"b', 'a\\\\\\"b'],
- ['a"b"c', 'a\\"b\\"c'],
- ['a""b', 'a\\"\\"b'],
- ['""', '\\"\\"'],
- ];
- for (const testCase of cases) {
- const [input, expected] = testCase;
- expect(escapeDoubleQuotes(input)).toBe(expected);
- }
- // should return the same input as the output
- const cases2 = ['ab', 'a\\"b', 'a\\\\\\"b'];
- for (const test of cases2) {
- expect(escapeDoubleQuotes(test)).toBe(test);
- }
- // don't unnecessarily escape
- const actual = escapeDoubleQuotes(escapeDoubleQuotes(escapeDoubleQuotes('a"b')));
- expect(actual).toBe('a\\"b');
- });
- });
|