From cb9a7f79d1c06eff40365b9ab7eb74cde29bd3f8 Mon Sep 17 00:00:00 2001
From: Evan Hahn <69474926+EvanHahn-Signal@users.noreply.github.com>
Date: Mon, 26 Oct 2020 17:15:28 -0500
Subject: [PATCH] Rewrite Message model tests in TypeScript
---
preload.js | 1 +
test/.eslintrc.js | 1 -
test/_test.js | 5 -
test/index.html | 1 -
.../test-electron/models/messages_test.ts | 123 +++++++++---------
ts/util/lint/exceptions.json | 56 +++++++-
6 files changed, 119 insertions(+), 68 deletions(-)
rename test/models/messages_test.js => ts/test-electron/models/messages_test.ts (86%)
diff --git a/preload.js b/preload.js
index d5f42d9b36..0b9f157b14 100644
--- a/preload.js
+++ b/preload.js
@@ -559,6 +559,7 @@ try {
};
/* eslint-disable global-require, import/no-extraneous-dependencies */
+ require('./ts/test-electron/models/messages_test');
require('./ts/test-electron/linkPreviews/linkPreviewFetch_test');
delete window.describe;
diff --git a/test/.eslintrc.js b/test/.eslintrc.js
index 0f2f1a02ff..d4428719bf 100644
--- a/test/.eslintrc.js
+++ b/test/.eslintrc.js
@@ -9,7 +9,6 @@ module.exports = {
globals: {
assert: true,
assertEqualArrayBuffers: true,
- clearDatabase: true,
dcodeIO: true,
getString: true,
hexToArrayBuffer: true,
diff --git a/test/_test.js b/test/_test.js
index 6e1e03ae23..7a74d90b16 100644
--- a/test/_test.js
+++ b/test/_test.js
@@ -77,10 +77,5 @@ before(async () => {
await window.storage.fetch();
});
-window.clearDatabase = async () => {
- await window.Signal.Data.removeAll();
- await window.storage.fetch();
-};
-
window.Whisper = window.Whisper || {};
window.Whisper.events = _.clone(Backbone.Events);
diff --git a/test/index.html b/test/index.html
index fb41bf32ed..00832f22ef 100644
--- a/test/index.html
+++ b/test/index.html
@@ -375,7 +375,6 @@
-
diff --git a/test/models/messages_test.js b/ts/test-electron/models/messages_test.ts
similarity index 86%
rename from test/models/messages_test.js
rename to ts/test-electron/models/messages_test.ts
index 11fa5460ed..cb9d120331 100644
--- a/test/models/messages_test.js
+++ b/ts/test-electron/models/messages_test.ts
@@ -1,38 +1,51 @@
-/* global ConversationController, i18n, Signal, Whisper, textsecure */
-
-'use strict';
-
-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();
-});
+import { assert } from 'chai';
+import * as sinon from 'sinon';
+import { setup as setupI18n } from '../../../js/modules/i18n';
+import enMessages from '../../../_locales/en/messages.json';
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();
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.
describe('send', () => {
it("saves the result's dataMessage", async () => {
@@ -72,28 +85,24 @@ describe('Message', () => {
it("triggers the 'sent' event on success", async () => {
const message = createMessage({ type: 'outgoing', source });
- const calls = [];
- message.on('sent', (...args) => {
- calls.push(args);
- });
+ const listener = sinon.spy();
+ message.on('sent', listener);
await message.send(Promise.resolve({}));
- assert.lengthOf(calls, 1);
- assert.strictEqual(calls[0][0], message);
+ sinon.assert.calledOnce(listener);
+ sinon.assert.calledWith(listener, message);
});
it("triggers the 'done' event on failure", async () => {
const message = createMessage({ type: 'outgoing', source });
- let callCount = 0;
- message.on('done', () => {
- callCount += 1;
- });
+ const listener = sinon.spy();
+ message.on('done', listener);
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 () => {
@@ -436,7 +445,8 @@ describe('Message', () => {
title: 'voice message',
attachment: {
contentType: 'audio/ogg',
- flags: textsecure.protobuf.AttachmentPointer.Flags.VOICE_MESSAGE,
+ flags:
+ window.textsecure.protobuf.AttachmentPointer.Flags.VOICE_MESSAGE,
},
expectedText: 'Voice Message',
expectedEmoji: '🎤',
@@ -523,15 +533,6 @@ describe('Message', () => {
});
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", () => {
assert.strictEqual(
createMessage({
@@ -543,8 +544,8 @@ describe('Message', () => {
);
});
- it("shows a notification's emoji on non-Linux", () => {
- Signal.OS.isLinux = () => false;
+ it("shows a notification's emoji on non-Linux", function test() {
+ this.sandbox.stub(window.Signal.OS, 'isLinux').returns(false);
assert.strictEqual(
createMessage({
@@ -560,8 +561,8 @@ describe('Message', () => {
);
});
- it('hides emoji on Linux', () => {
- Signal.OS.isLinux = () => true;
+ it('hides emoji on Linux', function test() {
+ this.sandbox.stub(window.Signal.OS, 'isLinux').returns(true);
assert.strictEqual(
createMessage({
@@ -594,19 +595,21 @@ describe('MessageCollection', () => {
it('should be ordered oldest to newest', () => {
const messages = new Whisper.MessageCollection();
// Timestamps
- const today = new Date();
- const tomorrow = new Date();
- tomorrow.setDate(today.getDate() + 1);
+ const today = Date.now();
+ const tomorrow = today + 12345;
// Add threads
messages.add({ received_at: today });
messages.add({ received_at: tomorrow });
const { models } = messages;
- const firstTimestamp = models[0].get('received_at').getTime();
- const secondTimestamp = models[1].get('received_at').getTime();
+ const firstTimestamp = models[0].get('received_at');
+ const secondTimestamp = models[1].get('received_at');
// 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!);
});
});
diff --git a/ts/util/lint/exceptions.json b/ts/util/lint/exceptions.json
index aa0b56305b..0684da523f 100644
--- a/ts/util/lint/exceptions.json
+++ b/ts/util/lint/exceptions.json
@@ -14900,6 +14900,60 @@
"reasonCategory": "falseMatch",
"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(",
"path": "ts/test/util/windowsZoneIdentifier_test.js",
@@ -15062,4 +15116,4 @@
"reasonCategory": "falseMatch",
"updated": "2020-09-08T23:07:22.682Z"
}
-]
\ No newline at end of file
+]