Rewrite Message model tests in TypeScript
This commit is contained in:
parent
8073ccd32c
commit
cb9a7f79d1
6 changed files with 119 additions and 68 deletions
|
@ -559,6 +559,7 @@ try {
|
||||||
};
|
};
|
||||||
|
|
||||||
/* eslint-disable global-require, import/no-extraneous-dependencies */
|
/* eslint-disable global-require, import/no-extraneous-dependencies */
|
||||||
|
require('./ts/test-electron/models/messages_test');
|
||||||
require('./ts/test-electron/linkPreviews/linkPreviewFetch_test');
|
require('./ts/test-electron/linkPreviews/linkPreviewFetch_test');
|
||||||
|
|
||||||
delete window.describe;
|
delete window.describe;
|
||||||
|
|
|
@ -9,7 +9,6 @@ module.exports = {
|
||||||
globals: {
|
globals: {
|
||||||
assert: true,
|
assert: true,
|
||||||
assertEqualArrayBuffers: true,
|
assertEqualArrayBuffers: true,
|
||||||
clearDatabase: true,
|
|
||||||
dcodeIO: true,
|
dcodeIO: true,
|
||||||
getString: true,
|
getString: true,
|
||||||
hexToArrayBuffer: true,
|
hexToArrayBuffer: true,
|
||||||
|
|
|
@ -77,10 +77,5 @@ before(async () => {
|
||||||
await window.storage.fetch();
|
await window.storage.fetch();
|
||||||
});
|
});
|
||||||
|
|
||||||
window.clearDatabase = async () => {
|
|
||||||
await window.Signal.Data.removeAll();
|
|
||||||
await window.storage.fetch();
|
|
||||||
};
|
|
||||||
|
|
||||||
window.Whisper = window.Whisper || {};
|
window.Whisper = window.Whisper || {};
|
||||||
window.Whisper.events = _.clone(Backbone.Events);
|
window.Whisper.events = _.clone(Backbone.Events);
|
||||||
|
|
|
@ -375,7 +375,6 @@
|
||||||
<script type="text/javascript" src="views/whisper_view_test.js"></script>
|
<script type="text/javascript" src="views/whisper_view_test.js"></script>
|
||||||
<script type="text/javascript" src="views/list_view_test.js"></script>
|
<script type="text/javascript" src="views/list_view_test.js"></script>
|
||||||
|
|
||||||
<script type="text/javascript" src="models/messages_test.js"></script>
|
|
||||||
<script type="text/javascript" src="models/conversations_test.js"></script>
|
<script type="text/javascript" src="models/conversations_test.js"></script>
|
||||||
|
|
||||||
<script type="text/javascript" src="libphonenumber_util_test.js"></script>
|
<script type="text/javascript" src="libphonenumber_util_test.js"></script>
|
||||||
|
|
|
@ -1,38 +1,51 @@
|
||||||
/* global ConversationController, i18n, Signal, Whisper, textsecure */
|
import { assert } from 'chai';
|
||||||
|
import * as sinon from 'sinon';
|
||||||
'use strict';
|
import { setup as setupI18n } from '../../../js/modules/i18n';
|
||||||
|
import enMessages from '../../../_locales/en/messages.json';
|
||||||
const attributes = {
|
|
||||||
type: 'outgoing',
|
|
||||||
body: 'hi',
|
|
||||||
conversationId: 'foo',
|
|
||||||
attachments: [],
|
|
||||||
received_at: new Date().getTime(),
|
|
||||||
};
|
|
||||||
|
|
||||||
const source = '+1 415-555-5555';
|
|
||||||
const me = '+14155555556';
|
|
||||||
const ourUuid = window.getGuid();
|
|
||||||
|
|
||||||
before(async () => {
|
|
||||||
await clearDatabase();
|
|
||||||
ConversationController.reset();
|
|
||||||
await ConversationController.load();
|
|
||||||
textsecure.storage.put('number_id', `${me}.2`);
|
|
||||||
textsecure.storage.put('uuid_id', `${ourUuid}.2`);
|
|
||||||
});
|
|
||||||
after(() => {
|
|
||||||
textsecure.storage.put('number_id', null);
|
|
||||||
textsecure.storage.put('uuid_id', null);
|
|
||||||
return clearDatabase();
|
|
||||||
});
|
|
||||||
|
|
||||||
describe('Message', () => {
|
describe('Message', () => {
|
||||||
function createMessage(attrs) {
|
const i18n = setupI18n('en', enMessages);
|
||||||
|
|
||||||
|
const attributes = {
|
||||||
|
type: 'outgoing',
|
||||||
|
body: 'hi',
|
||||||
|
conversationId: 'foo',
|
||||||
|
attachments: [],
|
||||||
|
received_at: new Date().getTime(),
|
||||||
|
};
|
||||||
|
|
||||||
|
const source = '+1 415-555-5555';
|
||||||
|
const me = '+14155555556';
|
||||||
|
const ourUuid = window.getGuid();
|
||||||
|
|
||||||
|
function createMessage(attrs: { [key: string]: unknown }) {
|
||||||
const messages = new Whisper.MessageCollection();
|
const messages = new Whisper.MessageCollection();
|
||||||
return messages.add(attrs);
|
return messages.add(attrs);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
before(async () => {
|
||||||
|
window.ConversationController.reset();
|
||||||
|
await window.ConversationController.load();
|
||||||
|
window.textsecure.storage.put('number_id', `${me}.2`);
|
||||||
|
window.textsecure.storage.put('uuid_id', `${ourUuid}.2`);
|
||||||
|
});
|
||||||
|
|
||||||
|
after(async () => {
|
||||||
|
window.textsecure.storage.put('number_id', null);
|
||||||
|
window.textsecure.storage.put('uuid_id', null);
|
||||||
|
|
||||||
|
await window.Signal.Data.removeAll();
|
||||||
|
await window.storage.fetch();
|
||||||
|
});
|
||||||
|
|
||||||
|
beforeEach(function beforeEach() {
|
||||||
|
this.sandbox = sinon.createSandbox();
|
||||||
|
});
|
||||||
|
|
||||||
|
afterEach(function afterEach() {
|
||||||
|
this.sandbox.restore();
|
||||||
|
});
|
||||||
|
|
||||||
// NOTE: These tests are incomplete.
|
// NOTE: These tests are incomplete.
|
||||||
describe('send', () => {
|
describe('send', () => {
|
||||||
it("saves the result's dataMessage", async () => {
|
it("saves the result's dataMessage", async () => {
|
||||||
|
@ -72,28 +85,24 @@ describe('Message', () => {
|
||||||
it("triggers the 'sent' event on success", async () => {
|
it("triggers the 'sent' event on success", async () => {
|
||||||
const message = createMessage({ type: 'outgoing', source });
|
const message = createMessage({ type: 'outgoing', source });
|
||||||
|
|
||||||
const calls = [];
|
const listener = sinon.spy();
|
||||||
message.on('sent', (...args) => {
|
message.on('sent', listener);
|
||||||
calls.push(args);
|
|
||||||
});
|
|
||||||
|
|
||||||
await message.send(Promise.resolve({}));
|
await message.send(Promise.resolve({}));
|
||||||
|
|
||||||
assert.lengthOf(calls, 1);
|
sinon.assert.calledOnce(listener);
|
||||||
assert.strictEqual(calls[0][0], message);
|
sinon.assert.calledWith(listener, message);
|
||||||
});
|
});
|
||||||
|
|
||||||
it("triggers the 'done' event on failure", async () => {
|
it("triggers the 'done' event on failure", async () => {
|
||||||
const message = createMessage({ type: 'outgoing', source });
|
const message = createMessage({ type: 'outgoing', source });
|
||||||
|
|
||||||
let callCount = 0;
|
const listener = sinon.spy();
|
||||||
message.on('done', () => {
|
message.on('done', listener);
|
||||||
callCount += 1;
|
|
||||||
});
|
|
||||||
|
|
||||||
await message.send(Promise.reject(new Error('something went wrong!')));
|
await message.send(Promise.reject(new Error('something went wrong!')));
|
||||||
|
|
||||||
assert.strictEqual(callCount, 1);
|
sinon.assert.calledOnce(listener);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('saves errors from promise rejections with errors', async () => {
|
it('saves errors from promise rejections with errors', async () => {
|
||||||
|
@ -436,7 +445,8 @@ describe('Message', () => {
|
||||||
title: 'voice message',
|
title: 'voice message',
|
||||||
attachment: {
|
attachment: {
|
||||||
contentType: 'audio/ogg',
|
contentType: 'audio/ogg',
|
||||||
flags: textsecure.protobuf.AttachmentPointer.Flags.VOICE_MESSAGE,
|
flags:
|
||||||
|
window.textsecure.protobuf.AttachmentPointer.Flags.VOICE_MESSAGE,
|
||||||
},
|
},
|
||||||
expectedText: 'Voice Message',
|
expectedText: 'Voice Message',
|
||||||
expectedEmoji: '🎤',
|
expectedEmoji: '🎤',
|
||||||
|
@ -523,15 +533,6 @@ describe('Message', () => {
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('getNotificationText', () => {
|
describe('getNotificationText', () => {
|
||||||
// Sinon isn't included in the Electron test setup so we do this.
|
|
||||||
beforeEach(function beforeEach() {
|
|
||||||
this.oldIsLinux = Signal.OS.isLinux;
|
|
||||||
});
|
|
||||||
|
|
||||||
afterEach(function afterEach() {
|
|
||||||
Signal.OS.isLinux = this.oldIsLinux;
|
|
||||||
});
|
|
||||||
|
|
||||||
it("returns a notification's text", () => {
|
it("returns a notification's text", () => {
|
||||||
assert.strictEqual(
|
assert.strictEqual(
|
||||||
createMessage({
|
createMessage({
|
||||||
|
@ -543,8 +544,8 @@ describe('Message', () => {
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
it("shows a notification's emoji on non-Linux", () => {
|
it("shows a notification's emoji on non-Linux", function test() {
|
||||||
Signal.OS.isLinux = () => false;
|
this.sandbox.stub(window.Signal.OS, 'isLinux').returns(false);
|
||||||
|
|
||||||
assert.strictEqual(
|
assert.strictEqual(
|
||||||
createMessage({
|
createMessage({
|
||||||
|
@ -560,8 +561,8 @@ describe('Message', () => {
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('hides emoji on Linux', () => {
|
it('hides emoji on Linux', function test() {
|
||||||
Signal.OS.isLinux = () => true;
|
this.sandbox.stub(window.Signal.OS, 'isLinux').returns(true);
|
||||||
|
|
||||||
assert.strictEqual(
|
assert.strictEqual(
|
||||||
createMessage({
|
createMessage({
|
||||||
|
@ -594,19 +595,21 @@ describe('MessageCollection', () => {
|
||||||
it('should be ordered oldest to newest', () => {
|
it('should be ordered oldest to newest', () => {
|
||||||
const messages = new Whisper.MessageCollection();
|
const messages = new Whisper.MessageCollection();
|
||||||
// Timestamps
|
// Timestamps
|
||||||
const today = new Date();
|
const today = Date.now();
|
||||||
const tomorrow = new Date();
|
const tomorrow = today + 12345;
|
||||||
tomorrow.setDate(today.getDate() + 1);
|
|
||||||
|
|
||||||
// Add threads
|
// Add threads
|
||||||
messages.add({ received_at: today });
|
messages.add({ received_at: today });
|
||||||
messages.add({ received_at: tomorrow });
|
messages.add({ received_at: tomorrow });
|
||||||
|
|
||||||
const { models } = messages;
|
const { models } = messages;
|
||||||
const firstTimestamp = models[0].get('received_at').getTime();
|
const firstTimestamp = models[0].get('received_at');
|
||||||
const secondTimestamp = models[1].get('received_at').getTime();
|
const secondTimestamp = models[1].get('received_at');
|
||||||
|
|
||||||
// Compare timestamps
|
// Compare timestamps
|
||||||
assert(firstTimestamp < secondTimestamp);
|
assert(typeof firstTimestamp === 'number');
|
||||||
|
assert(typeof secondTimestamp === 'number');
|
||||||
|
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
||||||
|
assert(firstTimestamp! < secondTimestamp!);
|
||||||
});
|
});
|
||||||
});
|
});
|
|
@ -14900,6 +14900,60 @@
|
||||||
"reasonCategory": "falseMatch",
|
"reasonCategory": "falseMatch",
|
||||||
"updated": "2020-02-07T19:52:28.522Z"
|
"updated": "2020-02-07T19:52:28.522Z"
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
"rule": "jQuery-before(",
|
||||||
|
"path": "ts/test-electron/models/messages_test.js",
|
||||||
|
"line": " before(async () => {",
|
||||||
|
"lineNumber": 33,
|
||||||
|
"reasonCategory": "testCode",
|
||||||
|
"updated": "2020-10-21T00:45:53.649Z",
|
||||||
|
"reasonDetail": "Test code and a false positive."
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"rule": "jQuery-load(",
|
||||||
|
"path": "ts/test-electron/models/messages_test.js",
|
||||||
|
"line": " await window.ConversationController.load();",
|
||||||
|
"lineNumber": 35,
|
||||||
|
"reasonCategory": "testCode",
|
||||||
|
"updated": "2020-10-21T00:45:53.649Z",
|
||||||
|
"reasonDetail": "Test code and a false positive."
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"rule": "jQuery-after(",
|
||||||
|
"path": "ts/test-electron/models/messages_test.js",
|
||||||
|
"line": " after(async () => {",
|
||||||
|
"lineNumber": 39,
|
||||||
|
"reasonCategory": "testCode",
|
||||||
|
"updated": "2020-10-21T00:45:53.649Z",
|
||||||
|
"reasonDetail": "Test code and a false positive."
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"rule": "jQuery-before(",
|
||||||
|
"path": "ts/test-electron/models/messages_test.ts",
|
||||||
|
"line": " before(async () => {",
|
||||||
|
"lineNumber": 26,
|
||||||
|
"reasonCategory": "testCode",
|
||||||
|
"updated": "2020-10-21T00:45:53.649Z",
|
||||||
|
"reasonDetail": "Test code and a false positive."
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"rule": "jQuery-load(",
|
||||||
|
"path": "ts/test-electron/models/messages_test.ts",
|
||||||
|
"line": " await window.ConversationController.load();",
|
||||||
|
"lineNumber": 28,
|
||||||
|
"reasonCategory": "testCode",
|
||||||
|
"updated": "2020-10-21T00:45:53.649Z",
|
||||||
|
"reasonDetail": "Test code and a false positive."
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"rule": "jQuery-after(",
|
||||||
|
"path": "ts/test-electron/models/messages_test.ts",
|
||||||
|
"line": " after(async () => {",
|
||||||
|
"lineNumber": 33,
|
||||||
|
"reasonCategory": "testCode",
|
||||||
|
"updated": "2020-10-21T00:45:53.649Z",
|
||||||
|
"reasonDetail": "Test code and a false positive."
|
||||||
|
},
|
||||||
{
|
{
|
||||||
"rule": "jQuery-before(",
|
"rule": "jQuery-before(",
|
||||||
"path": "ts/test/util/windowsZoneIdentifier_test.js",
|
"path": "ts/test/util/windowsZoneIdentifier_test.js",
|
||||||
|
@ -15062,4 +15116,4 @@
|
||||||
"reasonCategory": "falseMatch",
|
"reasonCategory": "falseMatch",
|
||||||
"updated": "2020-09-08T23:07:22.682Z"
|
"updated": "2020-09-08T23:07:22.682Z"
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
|
|
Loading…
Add table
Reference in a new issue