From d5b767c3747b9e7f9afcbcecffb662843ca2a346 Mon Sep 17 00:00:00 2001
From: Yamagishi Kazutoshi
Date: Mon, 16 Oct 2017 16:33:08 +0900
Subject: Replace JavaScript Testing Framework from Mocha to Jest (#5412)
---
.../__tests__/__snapshots__/avatar-test.js.snap | 33 ++++++
.../__snapshots__/avatar_overlay-test.js.snap | 24 ++++
.../__tests__/__snapshots__/button-test.js.snap | 114 ++++++++++++++++++
.../__snapshots__/display_name-test.js.snap | 23 ++++
.../mastodon/components/__tests__/avatar-test.js | 36 ++++++
.../components/__tests__/avatar_overlay-test.js | 29 +++++
.../mastodon/components/__tests__/button-test.js | 75 ++++++++++++
.../components/__tests__/display_name-test.js | 18 +++
.../features/emoji/__tests__/emoji-test.js | 61 ++++++++++
.../features/emoji/__tests__/emoji_index-test.js | 130 +++++++++++++++++++++
.../ui/components/__tests__/column-test.js | 34 ++++++
app/javascript/mastodon/test_setup.js | 5 +
12 files changed, 582 insertions(+)
create mode 100644 app/javascript/mastodon/components/__tests__/__snapshots__/avatar-test.js.snap
create mode 100644 app/javascript/mastodon/components/__tests__/__snapshots__/avatar_overlay-test.js.snap
create mode 100644 app/javascript/mastodon/components/__tests__/__snapshots__/button-test.js.snap
create mode 100644 app/javascript/mastodon/components/__tests__/__snapshots__/display_name-test.js.snap
create mode 100644 app/javascript/mastodon/components/__tests__/avatar-test.js
create mode 100644 app/javascript/mastodon/components/__tests__/avatar_overlay-test.js
create mode 100644 app/javascript/mastodon/components/__tests__/button-test.js
create mode 100644 app/javascript/mastodon/components/__tests__/display_name-test.js
create mode 100644 app/javascript/mastodon/features/emoji/__tests__/emoji-test.js
create mode 100644 app/javascript/mastodon/features/emoji/__tests__/emoji_index-test.js
create mode 100644 app/javascript/mastodon/features/ui/components/__tests__/column-test.js
create mode 100644 app/javascript/mastodon/test_setup.js
(limited to 'app/javascript')
diff --git a/app/javascript/mastodon/components/__tests__/__snapshots__/avatar-test.js.snap b/app/javascript/mastodon/components/__tests__/__snapshots__/avatar-test.js.snap
new file mode 100644
index 000000000..76ab3374a
--- /dev/null
+++ b/app/javascript/mastodon/components/__tests__/__snapshots__/avatar-test.js.snap
@@ -0,0 +1,33 @@
+// Jest Snapshot v1, https://goo.gl/fbAQLP
+
+exports[` Autoplay renders a animated avatar 1`] = `
+
+`;
+
+exports[` Still renders a still avatar 1`] = `
+
+`;
diff --git a/app/javascript/mastodon/components/__tests__/__snapshots__/avatar_overlay-test.js.snap b/app/javascript/mastodon/components/__tests__/__snapshots__/avatar_overlay-test.js.snap
new file mode 100644
index 000000000..d59fee42f
--- /dev/null
+++ b/app/javascript/mastodon/components/__tests__/__snapshots__/avatar_overlay-test.js.snap
@@ -0,0 +1,24 @@
+// Jest Snapshot v1, https://goo.gl/fbAQLP
+
+exports[`
+
+
+
+`;
diff --git a/app/javascript/mastodon/components/__tests__/__snapshots__/button-test.js.snap b/app/javascript/mastodon/components/__tests__/__snapshots__/button-test.js.snap
new file mode 100644
index 000000000..c3f018d90
--- /dev/null
+++ b/app/javascript/mastodon/components/__tests__/__snapshots__/button-test.js.snap
@@ -0,0 +1,114 @@
+// Jest Snapshot v1, https://goo.gl/fbAQLP
+
+exports[` adds class "button-secondary" if props.secondary given 1`] = `
+
+`;
+
+exports[` renders a button element 1`] = `
+
+`;
+
+exports[` renders a disabled attribute if props.disabled given 1`] = `
+
+`;
+
+exports[` renders class="button--block" if props.block given 1`] = `
+
+`;
+
+exports[` renders the children 1`] = `
+
+`;
+
+exports[` renders the given text 1`] = `
+
+`;
+
+exports[` renders the props.text instead of children 1`] = `
+
+`;
diff --git a/app/javascript/mastodon/components/__tests__/__snapshots__/display_name-test.js.snap b/app/javascript/mastodon/components/__tests__/__snapshots__/display_name-test.js.snap
new file mode 100644
index 000000000..533359ffe
--- /dev/null
+++ b/app/javascript/mastodon/components/__tests__/__snapshots__/display_name-test.js.snap
@@ -0,0 +1,23 @@
+// Jest Snapshot v1, https://goo.gl/fbAQLP
+
+exports[` renders display name + account name 1`] = `
+
+ Foo
",
+ }
+ }
+ />
+
+
+ @
+ bar@baz
+
+
+`;
diff --git a/app/javascript/mastodon/components/__tests__/avatar-test.js b/app/javascript/mastodon/components/__tests__/avatar-test.js
new file mode 100644
index 000000000..dd3f7b7d2
--- /dev/null
+++ b/app/javascript/mastodon/components/__tests__/avatar-test.js
@@ -0,0 +1,36 @@
+import React from 'react';
+import renderer from 'react-test-renderer';
+import { fromJS } from 'immutable';
+import Avatar from '../avatar';
+
+describe('', () => {
+ const account = fromJS({
+ username: 'alice',
+ acct: 'alice',
+ display_name: 'Alice',
+ avatar: '/animated/alice.gif',
+ avatar_static: '/static/alice.jpg',
+ });
+
+ const size = 100;
+
+ describe('Autoplay', () => {
+ it('renders a animated avatar', () => {
+ const component = renderer.create();
+ const tree = component.toJSON();
+
+ expect(tree).toMatchSnapshot();
+ });
+ });
+
+ describe('Still', () => {
+ it('renders a still avatar', () => {
+ const component = renderer.create();
+ const tree = component.toJSON();
+
+ expect(tree).toMatchSnapshot();
+ });
+ });
+
+ // TODO add autoplay test if possible
+});
diff --git a/app/javascript/mastodon/components/__tests__/avatar_overlay-test.js b/app/javascript/mastodon/components/__tests__/avatar_overlay-test.js
new file mode 100644
index 000000000..44addea83
--- /dev/null
+++ b/app/javascript/mastodon/components/__tests__/avatar_overlay-test.js
@@ -0,0 +1,29 @@
+import React from 'react';
+import renderer from 'react-test-renderer';
+import { fromJS } from 'immutable';
+import AvatarOverlay from '../avatar_overlay';
+
+describe(' {
+ const account = fromJS({
+ username: 'alice',
+ acct: 'alice',
+ display_name: 'Alice',
+ avatar: '/animated/alice.gif',
+ avatar_static: '/static/alice.jpg',
+ });
+
+ const friend = fromJS({
+ username: 'eve',
+ acct: 'eve@blackhat.lair',
+ display_name: 'Evelyn',
+ avatar: '/animated/eve.gif',
+ avatar_static: '/static/eve.jpg',
+ });
+
+ it('renders a overlay avatar', () => {
+ const component = renderer.create();
+ const tree = component.toJSON();
+
+ expect(tree).toMatchSnapshot();
+ });
+});
diff --git a/app/javascript/mastodon/components/__tests__/button-test.js b/app/javascript/mastodon/components/__tests__/button-test.js
new file mode 100644
index 000000000..160cd3cbc
--- /dev/null
+++ b/app/javascript/mastodon/components/__tests__/button-test.js
@@ -0,0 +1,75 @@
+import { shallow } from 'enzyme';
+import React from 'react';
+import renderer from 'react-test-renderer';
+import Button from '../button';
+
+describe('', () => {
+ it('renders a button element', () => {
+ const component = renderer.create();
+ const tree = component.toJSON();
+
+ expect(tree).toMatchSnapshot();
+ });
+
+ it('renders the given text', () => {
+ const text = 'foo';
+ const component = renderer.create();
+ const tree = component.toJSON();
+
+ expect(tree).toMatchSnapshot();
+ });
+
+ it('handles click events using the given handler', () => {
+ const handler = jest.fn();
+ const button = shallow();
+ button.find('button').simulate('click');
+
+ expect(handler.mock.calls.length).toEqual(1);
+ });
+
+ it('does not handle click events if props.disabled given', () => {
+ const handler = jest.fn();
+ const button = shallow();
+ button.find('button').simulate('click');
+
+ expect(handler.mock.calls.length).toEqual(0);
+ });
+
+ it('renders a disabled attribute if props.disabled given', () => {
+ const component = renderer.create();
+ const tree = component.toJSON();
+
+ expect(tree).toMatchSnapshot();
+ });
+
+ it('renders the children', () => {
+ const children = children
;
+ const component = renderer.create();
+ const tree = component.toJSON();
+
+ expect(tree).toMatchSnapshot();
+ });
+
+ it('renders the props.text instead of children', () => {
+ const text = 'foo';
+ const children = children
;
+ const component = renderer.create();
+ const tree = component.toJSON();
+
+ expect(tree).toMatchSnapshot();
+ });
+
+ it('renders class="button--block" if props.block given', () => {
+ const component = renderer.create();
+ const tree = component.toJSON();
+
+ expect(tree).toMatchSnapshot();
+ });
+
+ it('adds class "button-secondary" if props.secondary given', () => {
+ const component = renderer.create();
+ const tree = component.toJSON();
+
+ expect(tree).toMatchSnapshot();
+ });
+});
diff --git a/app/javascript/mastodon/components/__tests__/display_name-test.js b/app/javascript/mastodon/components/__tests__/display_name-test.js
new file mode 100644
index 000000000..0d040c4cd
--- /dev/null
+++ b/app/javascript/mastodon/components/__tests__/display_name-test.js
@@ -0,0 +1,18 @@
+import React from 'react';
+import renderer from 'react-test-renderer';
+import { fromJS } from 'immutable';
+import DisplayName from '../display_name';
+
+describe('', () => {
+ it('renders display name + account name', () => {
+ const account = fromJS({
+ username: 'bar',
+ acct: 'bar@baz',
+ display_name_html: 'Foo
',
+ });
+ const component = renderer.create();
+ const tree = component.toJSON();
+
+ expect(tree).toMatchSnapshot();
+ });
+});
diff --git a/app/javascript/mastodon/features/emoji/__tests__/emoji-test.js b/app/javascript/mastodon/features/emoji/__tests__/emoji-test.js
new file mode 100644
index 000000000..636402172
--- /dev/null
+++ b/app/javascript/mastodon/features/emoji/__tests__/emoji-test.js
@@ -0,0 +1,61 @@
+import emojify from '../emoji';
+
+describe('emoji', () => {
+ describe('.emojify', () => {
+ it('ignores unknown shortcodes', () => {
+ expect(emojify(':foobarbazfake:')).toEqual(':foobarbazfake:');
+ });
+
+ it('ignores shortcodes inside of tags', () => {
+ expect(emojify('')).toEqual('');
+ });
+
+ it('works with unclosed tags', () => {
+ expect(emojify('hello>')).toEqual('hello>');
+ expect(emojify(' {
+ expect(emojify('smile:')).toEqual('smile:');
+ expect(emojify(':smile')).toEqual(':smile');
+ });
+
+ it('does unicode', () => {
+ expect(emojify('\uD83D\uDC69\u200D\uD83D\uDC69\u200D\uD83D\uDC66\u200D\uD83D\uDC66')).toEqual(
+ '');
+ expect(emojify('๐จโ๐ฉโ๐งโ๐ง')).toEqual(
+ '');
+ expect(emojify('๐ฉโ๐ฉโ๐ฆ')).toEqual('');
+ expect(emojify('\u2757')).toEqual(
+ '');
+ });
+
+ it('does multiple unicode', () => {
+ expect(emojify('\u2757 #\uFE0F\u20E3')).toEqual(
+ ' ');
+ expect(emojify('\u2757#\uFE0F\u20E3')).toEqual(
+ '');
+ expect(emojify('\u2757 #\uFE0F\u20E3 \u2757')).toEqual(
+ ' ');
+ expect(emojify('foo \u2757 #\uFE0F\u20E3 bar')).toEqual(
+ 'foo bar');
+ });
+
+ it('ignores unicode inside of tags', () => {
+ expect(emojify('')).toEqual('');
+ });
+
+ it('does multiple emoji properly (issue 5188)', () => {
+ expect(emojify('๐๐๐')).toEqual('');
+ expect(emojify('๐ ๐ ๐')).toEqual(' ');
+ });
+
+ it('does an emoji that has no shortcode', () => {
+ expect(emojify('๐๏ธ')).toEqual('');
+ });
+
+ it('does an emoji whose filename is irregular', () => {
+ expect(emojify('โ๏ธ')).toEqual('');
+ });
+ });
+});
diff --git a/app/javascript/mastodon/features/emoji/__tests__/emoji_index-test.js b/app/javascript/mastodon/features/emoji/__tests__/emoji_index-test.js
new file mode 100644
index 000000000..53efa5743
--- /dev/null
+++ b/app/javascript/mastodon/features/emoji/__tests__/emoji_index-test.js
@@ -0,0 +1,130 @@
+import { pick } from 'lodash';
+import { emojiIndex } from 'emoji-mart';
+import { search } from '../emoji_mart_search_light';
+
+const trimEmojis = emoji => pick(emoji, ['id', 'unified', 'native', 'custom']);
+
+describe('emoji_index', () => {
+ it('should give same result for emoji_index_light and emoji-mart', () => {
+ const expected = [
+ {
+ id: 'pineapple',
+ unified: '1f34d',
+ native: '๐',
+ },
+ ];
+ expect(search('pineapple').map(trimEmojis)).toEqual(expected);
+ expect(emojiIndex.search('pineapple').map(trimEmojis)).toEqual(expected);
+ });
+
+ it('orders search results correctly', () => {
+ const expected = [
+ {
+ id: 'apple',
+ unified: '1f34e',
+ native: '๐',
+ },
+ {
+ id: 'pineapple',
+ unified: '1f34d',
+ native: '๐',
+ },
+ {
+ id: 'green_apple',
+ unified: '1f34f',
+ native: '๐',
+ },
+ {
+ id: 'iphone',
+ unified: '1f4f1',
+ native: '๐ฑ',
+ },
+ ];
+ expect(search('apple').map(trimEmojis)).toEqual(expected);
+ expect(emojiIndex.search('apple').map(trimEmojis)).toEqual(expected);
+ });
+
+ it('handles custom emoji', () => {
+ const custom = [
+ {
+ id: 'mastodon',
+ name: 'mastodon',
+ short_names: ['mastodon'],
+ text: '',
+ emoticons: [],
+ keywords: ['mastodon'],
+ imageUrl: 'http://example.com',
+ custom: true,
+ },
+ ];
+ search('', { custom });
+ emojiIndex.search('', { custom });
+ const expected = [
+ {
+ id: 'mastodon',
+ custom: true,
+ },
+ ];
+ expect(search('masto').map(trimEmojis)).toEqual(expected);
+ expect(emojiIndex.search('masto').map(trimEmojis)).toEqual(expected);
+ });
+
+ it('should filter only emojis we care about, exclude pineapple', () => {
+ const emojisToShowFilter = unified => unified !== '1F34D';
+ expect(search('apple', { emojisToShowFilter }).map((obj) => obj.id))
+ .not.toContain('pineapple');
+ expect(emojiIndex.search('apple', { emojisToShowFilter }).map((obj) => obj.id))
+ .not.toContain('pineapple');
+ });
+
+ it('can include/exclude categories', () => {
+ expect(search('flag', { include: ['people'] })).toEqual([]);
+ expect(emojiIndex.search('flag', { include: ['people'] })).toEqual([]);
+ });
+
+ it('does an emoji whose unified name is irregular', () => {
+ const expected = [
+ {
+ 'id': 'water_polo',
+ 'unified': '1f93d',
+ 'native': '๐คฝ',
+ },
+ {
+ 'id': 'man-playing-water-polo',
+ 'unified': '1f93d-200d-2642-fe0f',
+ 'native': '๐คฝโโ๏ธ',
+ },
+ {
+ 'id': 'woman-playing-water-polo',
+ 'unified': '1f93d-200d-2640-fe0f',
+ 'native': '๐คฝโโ๏ธ',
+ },
+ ];
+ expect(search('polo').map(trimEmojis)).toEqual(expected);
+ expect(emojiIndex.search('polo').map(trimEmojis)).toEqual(expected);
+ });
+
+ it('can search for thinking_face', () => {
+ const expected = [
+ {
+ id: 'thinking_face',
+ unified: '1f914',
+ native: '๐ค',
+ },
+ ];
+ expect(search('thinking_fac').map(trimEmojis)).toEqual(expected);
+ expect(emojiIndex.search('thinking_fac').map(trimEmojis)).toEqual(expected);
+ });
+
+ it('can search for woman-facepalming', () => {
+ const expected = [
+ {
+ id: 'woman-facepalming',
+ unified: '1f926-200d-2640-fe0f',
+ native: '๐คฆโโ๏ธ',
+ },
+ ];
+ expect(search('woman-facep').map(trimEmojis)).toEqual(expected);
+ expect(emojiIndex.search('woman-facep').map(trimEmojis)).toEqual(expected);
+ });
+});
diff --git a/app/javascript/mastodon/features/ui/components/__tests__/column-test.js b/app/javascript/mastodon/features/ui/components/__tests__/column-test.js
new file mode 100644
index 000000000..1e5e1d8dc
--- /dev/null
+++ b/app/javascript/mastodon/features/ui/components/__tests__/column-test.js
@@ -0,0 +1,34 @@
+import React from 'react';
+import { mount } from 'enzyme';
+import Column from '../column';
+import ColumnHeader from '../column_header';
+
+describe('', () => {
+ describe(' click handler', () => {
+ const originalRaf = global.requestAnimationFrame;
+
+ beforeEach(() => {
+ global.requestAnimationFrame = jest.fn();
+ });
+
+ afterAll(() => {
+ global.requestAnimationFrame = originalRaf;
+ });
+
+ it('runs the scroll animation if the column contains scrollable content', () => {
+ const wrapper = mount(
+
+
+
+ );
+ wrapper.find(ColumnHeader).simulate('click');
+ expect(global.requestAnimationFrame.mock.calls.length).toEqual(1);
+ });
+
+ it('does not try to scroll if there is no scrollable content', () => {
+ const wrapper = mount();
+ wrapper.find(ColumnHeader).simulate('click');
+ expect(global.requestAnimationFrame.mock.calls.length).toEqual(0);
+ });
+ });
+});
diff --git a/app/javascript/mastodon/test_setup.js b/app/javascript/mastodon/test_setup.js
new file mode 100644
index 000000000..80148379b
--- /dev/null
+++ b/app/javascript/mastodon/test_setup.js
@@ -0,0 +1,5 @@
+import { configure } from 'enzyme';
+import Adapter from 'enzyme-adapter-react-16';
+
+const adapter = new Adapter();
+configure({ adapter });
--
cgit