mattermost-mobile/test/test_helper.js
Harrison Healey 45f8a56d15 Fixed unit tests for the other changes I've made recently (#80)
* Removed mocking from client tests

* Updated Client tests to remove Client.setTeamId

* Fixed actions/general.test.js

* Updated test/reducer/channel.test.js for the updated store structure

* Commented out general reducer tests until the store structure is more finalized

* Properly used the team ID when creating a channel
2016-11-22 11:46:50 -05:00

127 lines
3 KiB
JavaScript

// Copyright (c) 2016 Mattermost, Inc. All Rights Reserved.
// See License.txt for license information.
import assert from 'assert';
import Client from 'client/client.js';
import Config from 'config/config.js';
const PASSWORD = 'password1';
class TestHelper {
constructor() {
this.basicClient = null;
this.basicUser = null;
this.basicTeam = null;
this.basicChannel = null;
this.basicPost = null;
}
assertStatusOkay = (data) => {
assert(data);
assert(data.status === 'OK');
}
generateId = () => {
// Implementation taken from http://stackoverflow.com/a/2117523
let id = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx';
id = id.replace(/[xy]/g, (c) => {
const r = Math.floor(Math.random() * 16);
let v;
if (c === 'x') {
v = r;
} else {
v = (r & 0x3) | 0x8;
}
return v.toString(16);
});
return 'uid' + id;
}
createClient = () => {
const client = new Client();
client.setUrl(Config.DefaultServerUrl);
return client;
}
fakeEmail = () => {
return 'success' + this.generateId() + '@simulator.amazonses.com';
}
fakeUser = () => {
return {
email: this.fakeEmail(),
allow_marketing: true,
password: PASSWORD,
username: this.generateId()
};
}
fakeTeam = () => {
const name = this.generateId();
return {
name,
display_name: `Unit Test ${name}`,
type: 'O',
email: this.fakeEmail(),
allowed_domains: ''
};
}
fakeChannel = (teamId) => {
const name = this.generateId();
return {
name,
team_id: teamId,
display_name: `Unit Test ${name}`,
type: 'O'
};
}
fakeChannelMember = (userId, channelId) => {
return {
user_id: userId,
channel_id: channelId,
notify_props: {},
roles: 'system_user'
};
}
fakePost = (channelId) => {
return {
channel_id: channelId,
message: `Unit Test ${this.generateId()}`
};
}
initBasic = async (client = this.createClient()) => {
client.setUrl(Config.DefaultServerUrl);
this.basicClient = client;
this.basicUser = await client.createUser(this.fakeUser());
await client.login(this.basicUser.email, PASSWORD);
this.basicTeam = await client.createTeam(this.fakeTeam());
this.basicChannel = await client.createChannel(this.fakeChannel(this.basicTeam.id));
this.basicPost = await client.createPost(this.basicTeam.id, this.fakePost(this.basicChannel.id));
return {
client: this.basicClient,
user: this.basicUser,
team: this.basicTeam,
channel: this.basicChannel,
post: this.basicPost
};
}
}
export default new TestHelper();