Skip to content

Commit

Permalink
feat(test): keep testing
Browse files Browse the repository at this point in the history
  • Loading branch information
leeyeh committed Feb 22, 2016
1 parent 198439f commit 37dcd23
Show file tree
Hide file tree
Showing 8 changed files with 304 additions and 62 deletions.
13 changes: 5 additions & 8 deletions src/connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,10 @@ export default class Connection extends WebSocketPlus {
message.i = this._serialId;
debug(trim(message), 'sent');

if (message.toArrayBuffer) {
message = message.toArrayBuffer();
} else if (message.toBuffer) {
if (message.toBuffer) {
message = message.toBuffer();
} else if (message.toArrayBuffer) {
message = message.toArrayBuffer();
} else {
throw new TypeError(`${message} is not a GenericCommand`);
}
Expand Down Expand Up @@ -79,7 +79,6 @@ export default class Connection extends WebSocketPlus {
}

handleMessage(msg) {
if (msg === '{}') return;
let message;
try {
message = GenericCommand.decode(msg);
Expand Down Expand Up @@ -112,10 +111,8 @@ export default class Connection extends WebSocketPlus {
}

ping() {
this.send(new GenericCommand({
// cmd: 'echo',
cmd: 'session',
op: 'open',
return this.send(new GenericCommand({
cmd: 'echo',
}));
}
}
12 changes: 7 additions & 5 deletions src/conversation-query.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ export default class ConversationQuery {
if (value instanceof Date) {
return { __type: 'Date', iso: value.toJSON() };
}
if (value instanceof RegExp) {
return value.source;
}
return value;
}

Expand All @@ -15,10 +18,10 @@ export default class ConversationQuery {

static _calculateFlag(options) {
return [
'compact',
'withLastMessages',
'compact',
].reduce(
(prev, key) => prev >> 1 + (Boolean)(options[key]),
(prev, key) => (prev << 1) + (Boolean)(options[key]),
0
);
}
Expand Down Expand Up @@ -55,9 +58,8 @@ export default class ConversationQuery {
* @param peerIds
* @return
*/

containsMembers(peerIds) {
return this._containsAll('m', peerIds);
return this.containsAll('m', peerIds);
}

/**
Expand All @@ -73,7 +75,7 @@ export default class ConversationQuery {
peerIdsSet.add(this._client.id);
}
this.sizeEqualTo('m', peerIdsSet.size);
return this.containsMembers(peerIdsSet);
return this.containsMembers(Array.from(peerIdsSet));
}

/**
Expand Down
1 change: 1 addition & 0 deletions src/im-client.js
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ export default class IMClient extends Client {
const data = keyRemap({
objectId: 'id',
lm: 'lastMessageAt',
msg: 'lastMessage',
m: 'members',
attr: 'attributes',
tr: 'isTransient',
Expand Down
2 changes: 2 additions & 0 deletions src/realtime.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ export default class Realtime extends EventEmitter {
appId,
region,
ssl,
_debug,
} = options;
let router;
switch (region) {
Expand All @@ -115,6 +116,7 @@ export default class Realtime extends EventEmitter {
.query({
appId,
secure: ssl,
debug: _debug,
_t: Date.now(),
})
.timeout(20000)
Expand Down
262 changes: 262 additions & 0 deletions test/conversation-query.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,262 @@
import ConversationQuery from '../src/conversation-query';
import Realtime from '../src/realtime';

const APP_ID = process.env.APP_ID || 'anruhhk6visejjip57psvv5uuv8sggrzdfl9pg2bghgsiy35';
const APP_KEY = process.env.APP_KEY || 'xhiibo2eiyokjdu2y3kqcb7334rtw4x33zam98buxzkjuq5g';
const REGION = process.env.REGION || 'cn';
const EXISTING_ROOM_ID = process.env.EXSITING_ROOM_ID || '559d08a1e4b0a35bc5062ba1';
const CLIENT_ID = 'leeyeh';

describe('IMClient', () => {
let client;
before(() =>
new Realtime({
appId: APP_ID,
appKey: APP_KEY,
region: REGION,
pushUnread: false,
})
.createIMClient(CLIENT_ID)
.then(c => (client = c))
);

describe('query', () => {
describe('ConversationQuery', () => {
it('_calculateFlag', () => {
const calculate = ConversationQuery._calculateFlag;
calculate({}).should.be.equal(0);
calculate({
compact: true,
withLastMessages: true,
}).should.be.equal(3);
});
});
it('should be a ConversationQuery', () => {
client.getQuery().should.be.instanceof(ConversationQuery);
});
it('equalTo', () =>
client.getQuery().equalTo('objectId', EXISTING_ROOM_ID).find()
.then(conversations => {
conversations.length.should.be.equal(1);
conversations[0].id.should.be.equal(EXISTING_ROOM_ID);
})
);
it('containsMembers', () =>
Promise.all([
client.getQuery().equalTo('objectId', EXISTING_ROOM_ID)
.containsMembers(['hjiang'])
.find()
.then(conversations => {
conversations.length.should.be.equal(1);
conversations[0].id.should.be.equal(EXISTING_ROOM_ID);
}),
client.getQuery().equalTo('objectId', EXISTING_ROOM_ID)
.containsMembers(['nobody'])
.find()
.then(conversations => {
conversations.length.should.be.equal(0);
}),
])
);
it('withMembers', () =>
Promise.all([
client.getQuery().equalTo('objectId', EXISTING_ROOM_ID)
.withMembers(['hjiang'], true)
.find()
.then(conversations => {
conversations.length.should.be.equal(1);
conversations[0].id.should.be.equal(EXISTING_ROOM_ID);
}),
client.getQuery().equalTo('objectId', EXISTING_ROOM_ID)
.withMembers(['hjiang'])
.find()
.then(conversations => {
conversations.length.should.be.equal(0);
}),
])
);
it('notEqualTo', () =>
Promise.all([
client.getQuery().equalTo('objectId', EXISTING_ROOM_ID)
.notEqualTo('name', 'not-this-name')
.find()
.then(conversations => {
conversations.length.should.be.equal(1);
conversations[0].id.should.be.equal(EXISTING_ROOM_ID);
}),
client.getQuery().equalTo('objectId', EXISTING_ROOM_ID)
.notEqualTo('name', 'js-realtime-sdk-testconv')
.find()
.then(conversations => {
conversations.length.should.be.equal(0);
}),
])
);
it('matches', () =>
Promise.all([
client.getQuery().equalTo('objectId', EXISTING_ROOM_ID)
.matches('name', /REALTIME-SDK/i)
.find()
.then(conversations => {
conversations.length.should.be.equal(1);
conversations[0].id.should.be.equal(EXISTING_ROOM_ID);
}),
client.getQuery().equalTo('objectId', EXISTING_ROOM_ID)
.matches('name', /REALTIME-SDK/)
.find()
.then(conversations => {
conversations.length.should.be.equal(0);
}),
])
);
it('contains', () =>
Promise.all([
client.getQuery().equalTo('objectId', EXISTING_ROOM_ID)
.contains('name', 'realtime-sdk')
.find()
.then(conversations => {
conversations.length.should.be.equal(1);
conversations[0].id.should.be.equal(EXISTING_ROOM_ID);
}),
client.getQuery().equalTo('objectId', EXISTING_ROOM_ID)
.contains('name', 'REALTIME-SDK')
.find()
.then(conversations => {
conversations.length.should.be.equal(0);
}),
])
);
it('startsWith', () =>
Promise.all([
client.getQuery().equalTo('objectId', EXISTING_ROOM_ID)
.startsWith('name', 'js-realtime')
.find()
.then(conversations => {
conversations.length.should.be.equal(1);
conversations[0].id.should.be.equal(EXISTING_ROOM_ID);
}),
client.getQuery().equalTo('objectId', EXISTING_ROOM_ID)
.startsWith('name', 'JS-REALTIME')
.find()
.then(conversations => {
conversations.length.should.be.equal(0);
}),
])
);
it('endsWith', () =>
Promise.all([
client.getQuery().equalTo('objectId', EXISTING_ROOM_ID)
.endsWith('name', 'testconv')
.find()
.then(conversations => {
conversations.length.should.be.equal(1);
conversations[0].id.should.be.equal(EXISTING_ROOM_ID);
}),
client.getQuery().equalTo('objectId', EXISTING_ROOM_ID)
.endsWith('name', 'TESTCONV')
.find()
.then(conversations => {
conversations.length.should.be.equal(0);
}),
])
);
it('lessThan', () =>
Promise.all([
client.getQuery().equalTo('objectId', EXISTING_ROOM_ID)
.lessThan('createdAt', new Date())
.find()
.then(conversations => {
conversations.length.should.be.equal(1);
conversations[0].id.should.be.equal(EXISTING_ROOM_ID);
}),
client.getQuery().equalTo('objectId', EXISTING_ROOM_ID)
.lessThan('createdAt', new Date(0))
.find()
.then(conversations => {
conversations.length.should.be.equal(0);
}),
])
);
it('lessThanOrEqualTo', () =>
Promise.all([
client.getQuery().equalTo('objectId', EXISTING_ROOM_ID)
.lessThanOrEqualTo('createdAt', new Date())
.find()
.then(conversations => {
conversations.length.should.be.equal(1);
conversations[0].id.should.be.equal(EXISTING_ROOM_ID);
}),
client.getQuery().equalTo('objectId', EXISTING_ROOM_ID)
.lessThanOrEqualTo('createdAt', new Date(0))
.find()
.then(conversations => {
conversations.length.should.be.equal(0);
}),
])
);
it('greaterThan', () =>
Promise.all([
client.getQuery().equalTo('objectId', EXISTING_ROOM_ID)
.greaterThan('createdAt', new Date(0))
.find()
.then(conversations => {
conversations.length.should.be.equal(1);
conversations[0].id.should.be.equal(EXISTING_ROOM_ID);
}),
client.getQuery().equalTo('objectId', EXISTING_ROOM_ID)
.greaterThan('createdAt', new Date())
.find()
.then(conversations => {
conversations.length.should.be.equal(0);
}),
])
);
it('greaterThanOrEqualTo', () =>
Promise.all([
client.getQuery().equalTo('objectId', EXISTING_ROOM_ID)
.greaterThanOrEqualTo('createdAt', new Date(0))
.find()
.then(conversations => {
conversations.length.should.be.equal(1);
conversations[0].id.should.be.equal(EXISTING_ROOM_ID);
}),
client.getQuery().equalTo('objectId', EXISTING_ROOM_ID)
.greaterThanOrEqualTo('createdAt', new Date())
.find()
.then(conversations => {
conversations.length.should.be.equal(0);
}),
])
);
it('limit', () =>
client.getQuery().equalTo('objectId', EXISTING_ROOM_ID)
.limit(0)
.find()
.then(conversations => conversations.length.should.be.equal(0))
);
it('skip', () =>
client.getQuery().equalTo('objectId', EXISTING_ROOM_ID)
.skip(1)
.find()
.then(conversations => conversations.length.should.be.equal(0))
);
it('compact', () =>
client.getQuery().equalTo('objectId', EXISTING_ROOM_ID)
.compact(true)
.find()
.then(conversations => {
conversations.length.should.be.equal(1);
conversations[0].members.length.should.be.equal(0);
})
);
it('withLastMessages', () =>
client.getQuery().equalTo('objectId', EXISTING_ROOM_ID)
.withLastMessages(true)
.find()
.then(conversations => {
conversations.length.should.be.equal(1);
conversations[0].should.have.property('lastMessage');
})
);
});
});
Loading

0 comments on commit 37dcd23

Please sign in to comment.