Skip to content

Commit

Permalink
test: fixing unstable tests and moving old
Browse files Browse the repository at this point in the history
  • Loading branch information
Mateusz Russak committed Dec 16, 2023
1 parent 6b8777d commit 82cf881
Show file tree
Hide file tree
Showing 10 changed files with 56 additions and 47 deletions.
2 changes: 1 addition & 1 deletion packages/repo/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export * from './channel/channelTypes';
export const createRepositories = (databaseUrl) => {
const client = init(databaseUrl);

Check warning on line 21 in packages/repo/src/index.ts

View workflow job for this annotation

GitHub Actions / Tests

'client' is assigned a value but never used. Allowed unused vars must match /^_/u
return {
db: connect().then(({ db }) => db),
connect,
ObjectId,
message: new MessageRepo(),
channel: new ChannelRepo(),
Expand Down
1 change: 1 addition & 0 deletions packages/server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"dev": "nodemon src/index.js",
"lint": "eslint --ext .ts,.js .",
"test": "NODE_ENV='test' DATABASE_URL='mongodb://chat:chat@localhost:27017/tests?authSource=admin' mocha --watch",
"testx": "NODE_ENV='test' DATABASE_URL='mongodb://chat:chat@localhost:27017/tests?authSource=admin' mocha",
"test:ci": "NODE_ENV='test' mocha --exit"
},
"author": "",
Expand Down
10 changes: 5 additions & 5 deletions packages/server/src/app/actions/message/tests/pins.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ describe('messages:pins', () => {
assert.equal(res.type, 'response');
assert.equal(res.status, 'ok');
assert.equal(res.count, 1);
assert.equal(msg.flat, 'Hello pinned');
assert.equal(msg.flat, 'Hello');
});

it('should have no access to someones private channel', async () => {
Expand All @@ -38,22 +38,22 @@ describe('messages:pins', () => {
assert.equal(res.message, 'ACCESS_DENIED');
});

it('should ', async () => {
it('should return continuation if after is specified', async () => {
const { res, data: [msg1, msg2] } = await api.sendMessage({
type: 'messages:pins',
channelId: channel.id,
limit: 2,
}, { userId: user.id });
assert.equal(res.type, 'response');
assert.equal(res.status, 'ok');
const { res: res2, data: [msg] } = await api.sendMessage({
const { res: res2, data: msgs } = await api.sendMessage({
type: 'messages:pins',
channelId: channel.id,
after: msg1.id,
limit: 1,
limit: 10,
}, { userId: user.id });
assert.equal(res2.type, 'response');
assert.equal(res2.status, 'ok');
assert.equal(msg.id, msg2.id);
assert.equal(msgs[0].id, msg2.id);
});
});
5 changes: 3 additions & 2 deletions packages/server/src/app/actions/message/tests/seeds.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ module.exports = {
}
const testChannel = await api.repo.channel.get({ name: 'test' });
if (!testChannel) await api.repo.channel.create({ name: 'test', private: false });
await api.repo.message.removeMany({ flat: 'Hello' });
const {db} = await api.repo.connect();

Check failure on line 15 in packages/server/src/app/actions/message/tests/seeds.js

View workflow job for this annotation

GitHub Actions / Tests

A space is required after '{'

Check failure on line 15 in packages/server/src/app/actions/message/tests/seeds.js

View workflow job for this annotation

GitHub Actions / Tests

A space is required before '}'
db.collection('messages').deleteMany({});
await api.repo.badge.removeMany({});
await api.repo.message.createMany([
{
Expand Down Expand Up @@ -70,7 +71,7 @@ module.exports = {
pinned: true,
flat: 'Hello',
userId: member.id,
createdAt: new Date('2022-01-05'),
createdAt: new Date('2022-01-06'),
},
]);
},
Expand Down
28 changes: 28 additions & 0 deletions packages/server/src/app/actions/tests/fcmSetup.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
const assert = require('assert');
const api = require('./api');

describe('fcm:setup', () => {
let user;
before(async () => {
await api.repo.user.update({ login: 'admin' }, { notifications: {} });
user = await api.repo.user.get({ login: 'admin' });
});
it('should update fcm token for current session', async () => {
const token = 'testToken';
const { res } = await api.sendMessage({
type: 'fcm:setup',
token,
}, { userId: user.id });
assert.equal(res.type, 'response');
assert.equal(res.status, 'ok');
const updatedUser = await api.repo.user.get({ id: user.id });
assert.ok(updatedUser.notifications[token]);
});
it('should throw error when token is not present', async () => {
const { res } = await api.sendMessage({
type: 'fcm:setup',
}).catch((e) => e);
assert.equal(res.status, 'error');
assert.equal(res.message, '"token" is required');
});
});
1 change: 0 additions & 1 deletion packages/server/tests/actions/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
module.exports = (connect) => {
describe('actions', () => {
require('./message.spec')(connect);
require('./setupFcm.spec')(connect);
require('./typing.spec')(connect);
});
};
4 changes: 4 additions & 0 deletions packages/server/tests/actions/message.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ module.exports = (connect) => {
before(async () => {
channel = await repo.channel.get({ name: 'main' });
});

after(async () => {
await repo.close();
});
it('should be received by other users', async () => {
const member = await connect('member');
const admin = await connect('admin');
Expand Down
29 changes: 0 additions & 29 deletions packages/server/tests/actions/setupFcm.spec.js

This file was deleted.

4 changes: 4 additions & 0 deletions packages/server/tests/actions/typing.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ module.exports = (connect) => {
channel = await repo.channel.get({ name: 'main' });
});

after(async () => {
await repo.close();
});

it('should be received by other users', async () => {
const member = await connect('member');
const admin = await connect('admin');
Expand Down
19 changes: 10 additions & 9 deletions packages/server/tests/environment.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
process.env.DATABASE_URL = 'mongodb://chat:chat@localhost:27017/tests?authSource=admin';

const { db } = require('../src/infra/repositories');
const { connect } = require('../src/infra/repositories');

exports.mochaHooks = {
beforeAll: async () => {
const member = (await db).collection('users').findOne({ login: 'member' });
const admin = (await db).collection('users').findOne({ login: 'admin' });
const channel = await (await db).collection('channels').findOne({ name: 'main' });
const testChannel = await (await db).collection('channels').findOne({ name: 'test' });
if (!testChannel) await (await db).collection('channels').insertOne({ name: 'test', private: false });
await (await db).collection('messages').deleteMany({});
await (await db).collection('badges').deleteMany({});
await (await db).collection('messages').insertMany([
const { db } = await connect();
const member = db.collection('users').findOne({ login: 'member' });
const admin = db.collection('users').findOne({ login: 'admin' });
const channel = await db.collection('channels').findOne({ name: 'main' });
const testChannel = await db.collection('channels').findOne({ name: 'test' });
if (!testChannel) await db.collection('channels').insertOne({ name: 'test', private: false });
await db.collection('messages').deleteMany({});
await db.collection('badges').deleteMany({});
await db.collection('messages').insertMany([
{
clientId: 1 + (`${Math.random()}`).slice(2),
message: { line: { text: 'Hello' } },
Expand Down

0 comments on commit 82cf881

Please sign in to comment.