Rewrite Message model tests in TypeScript

This commit is contained in:
Evan Hahn 2020-10-26 17:15:28 -05:00 committed by Evan Hahn
parent 8073ccd32c
commit cb9a7f79d1
6 changed files with 119 additions and 68 deletions

View File

@ -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;

View File

@ -9,7 +9,6 @@ module.exports = {
globals: {
assert: true,
assertEqualArrayBuffers: true,
clearDatabase: true,
dcodeIO: true,
getString: true,
hexToArrayBuffer: true,

View File

@ -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);

View File

@ -375,7 +375,6 @@
<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="models/messages_test.js"></script>
<script type="text/javascript" src="models/conversations_test.js"></script>
<script type="text/javascript" src="libphonenumber_util_test.js"></script>

View File

@ -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!);
});
});

View File

@ -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"
}
]
]