From 7d449e05562c76bb430efc1c36bd715675a84152 Mon Sep 17 00:00:00 2001
From: George Goldberg <george@gberg.me>
Date: Thu, 23 Mar 2017 18:05:36 +0000
Subject: PLT-5755: Infrastructure for Component Testing. (#5814)

This migrates the existing webapp tests to using Jest and Enzyme. The
infrastructure is put in place for React component testing, and a few
simple example component tests are implemented.

This also adds snapshot testing of components, coverage checking for the
webapp (although that is not yet integrated to Coveralls), and the
ability to run npm run test:watch to automatically re-run affected tests
when working on the webapp codebase.
---
 webapp/tests/helpers/client-test-helper.jsx | 176 ++++++++++++++++++++++++++++
 1 file changed, 176 insertions(+)
 create mode 100644 webapp/tests/helpers/client-test-helper.jsx

(limited to 'webapp/tests/helpers/client-test-helper.jsx')

diff --git a/webapp/tests/helpers/client-test-helper.jsx b/webapp/tests/helpers/client-test-helper.jsx
new file mode 100644
index 000000000..24aa5e281
--- /dev/null
+++ b/webapp/tests/helpers/client-test-helper.jsx
@@ -0,0 +1,176 @@
+// Copyright (c) 2016 Mattermost, Inc. All Rights Reserved.
+// See License.txt for license information.
+
+import Client from 'client/client.jsx';
+import WebSocketClient from 'client/websocket_client.jsx';
+import jqd from 'jquery-deferred';
+
+var HEADER_TOKEN = 'token';
+
+class TestHelperClass {
+    basicClient = () => {
+        return this.basicc;
+    }
+
+    basicWebSocketClient = () => {
+        return this.basicwsc;
+    }
+
+    basicTeam = () => {
+        return this.basict;
+    }
+
+    basicUser = () => {
+        return this.basicu;
+    }
+
+    basicChannel = () => {
+        return this.basicch;
+    }
+
+    basicPost = () => {
+        return this.basicp;
+    }
+
+    generateId = () => {
+        // implementation taken from http://stackoverflow.com/a/2117523
+        var id = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx';
+
+        id = id.replace(/[xy]/g, function replaceRandom(c) {
+            var r = Math.floor(Math.random() * 16);
+
+            var v;
+            if (c === 'x') {
+                v = r;
+            } else {
+                v = (r & 0x3) | 0x8;
+            }
+
+            return v.toString(16);
+        });
+
+        return 'uid' + id;
+    }
+
+    createClient() {
+        var c = new Client();
+        c.setUrl('http://localhost:8065');
+        c.useHeaderToken();
+        c.enableLogErrorsToConsole(true);
+        return c;
+    }
+
+    createWebSocketClient(token) {
+        var ws = new WebSocketClient();
+        ws.initialize('http://localhost:8065/api/v3/users/websocket', token);
+        return ws;
+    }
+
+    fakeEmail = () => {
+        return 'success' + this.generateId() + '@simulator.amazonses.com';
+    }
+
+    fakeUser = () => {
+        var user = {};
+        user.email = this.fakeEmail();
+        user.allow_marketing = true;
+        user.password = 'password1';
+        user.username = this.generateId();
+        return user;
+    }
+
+    fakeTeam = () => {
+        var team = {};
+        team.name = this.generateId();
+        team.display_name = `Unit Test ${team.name}`;
+        team.type = 'O';
+        team.email = this.fakeEmail();
+        team.allowed_domains = '';
+        return team;
+    }
+
+    fakeChannel = () => {
+        var channel = {};
+        channel.name = this.generateId();
+        channel.display_name = `Unit Test ${channel.name}`;
+        channel.type = 'O'; // open channel
+        return channel;
+    }
+
+    fakePost = () => {
+        var post = {};
+        post.message = `Unit Test ${this.generateId()}`;
+        return post;
+    }
+
+    initBasic = (done, callback, connectWS) => {
+        this.basicc = this.createClient();
+
+        function throwerror(err) {
+            done.fail(new Error(err.message));
+        }
+
+        var d1 = jqd.Deferred();
+        var email = this.fakeEmail();
+        var user = this.fakeUser();
+        var team = this.fakeTeam();
+        team.email = email;
+        user.email = email;
+        var self = this;
+
+        this.basicClient().createUser(
+            user,
+            function(ruser) {
+                self.basicu = ruser;
+                self.basicu.password = user.password;
+                self.basicClient().login(
+                    self.basicu.email,
+                    self.basicu.password,
+                    null,
+                    function(data, res) {
+                        if (connectWS) {
+                            self.basicwsc = self.createWebSocketClient(res.header[HEADER_TOKEN]);
+                        }
+                        self.basicClient().useHeaderToken();
+                        self.basicClient().createTeam(team,
+                            function(rteam) {
+                                self.basict = rteam;
+                                self.basicClient().setTeamId(rteam.id);
+                                var channel = self.fakeChannel();
+                                channel.team_id = self.basicTeam().id;
+                                self.basicClient().createChannel(
+                                    channel,
+                                    function(rchannel) {
+                                        self.basicch = rchannel;
+                                        var post = self.fakePost();
+                                        post.channel_id = rchannel.id;
+
+                                        self.basicClient().createPost(
+                                            post,
+                                            function(rpost) {
+                                                self.basicp = rpost;
+                                                d1.resolve();
+                                            },
+                                            throwerror
+                                        );
+                                    },
+                                    throwerror
+                                );
+                            },
+                            throwerror
+                        );
+                    },
+                    throwerror
+                );
+            },
+            throwerror
+        );
+
+        jqd.when(d1).done(() => {
+            callback();
+        });
+    }
+}
+
+var TestHelper = new TestHelperClass();
+export default TestHelper;
-- 
cgit v1.2.3-1-g7c22

