1
0
mirror of https://github.com/matrix-org/matrix-js-sdk.git synced 2025-11-28 05:03:59 +03:00

Units! Tests! Linting!

This commit is contained in:
Kegan Dougal
2015-10-19 15:24:24 +01:00
parent a236219111
commit 2c213f88d9
6 changed files with 87 additions and 12 deletions

View File

@@ -3,6 +3,7 @@
*/ */
var utils = require("./utils"); var utils = require("./utils");
/** Content Repo utility functions */
module.exports = { module.exports = {
/** /**
* Get the HTTP URL for an MXC URI. * Get the HTTP URL for an MXC URI.
@@ -55,8 +56,8 @@ module.exports = {
* Get an identicon URL from an arbitrary string. * Get an identicon URL from an arbitrary string.
* @param {string} baseUrl The base homeserver url which has a content repo. * @param {string} baseUrl The base homeserver url which has a content repo.
* @param {string} identiconString The string to create an identicon for. * @param {string} identiconString The string to create an identicon for.
* @param {Number} width The desired width of the image in pixels. * @param {Number} width The desired width of the image in pixels. Default: 96.
* @param {Number} height The desired height of the image in pixels. * @param {Number} height The desired height of the image in pixels. Default: 96.
* @return {string} The complete URL to the identicon. * @return {string} The complete URL to the identicon.
*/ */
getIdenticonUri: function(baseUrl, identiconString, width, height) { getIdenticonUri: function(baseUrl, identiconString, width, height) {

View File

@@ -163,11 +163,11 @@ RoomMember.prototype.getLastModifiedTime = function() {
*/ */
RoomMember.prototype.getAvatarUrl = RoomMember.prototype.getAvatarUrl =
function(baseUrl, width, height, resizeMethod, allowDefault) { function(baseUrl, width, height, resizeMethod, allowDefault) {
if (!this.events.member) { if (allowDefault === undefined) { allowDefault = true; }
if (!this.events.member && !allowDefault) {
return null; return null;
} }
if (allowDefault === undefined) { allowDefault = true; } var rawUrl = this.events.member ? this.events.member.getContent().avatar_url : null;
var rawUrl = this.events.member.getContent().avatar_url;
if (rawUrl) { if (rawUrl) {
return ContentRepo.getHttpUriForMxc( return ContentRepo.getHttpUriForMxc(
baseUrl, rawUrl, width, height, resizeMethod baseUrl, rawUrl, width, height, resizeMethod
@@ -175,7 +175,7 @@ RoomMember.prototype.getAvatarUrl =
} }
else if (allowDefault) { else if (allowDefault) {
return ContentRepo.getIdenticonUri( return ContentRepo.getIdenticonUri(
baseUrl, member.userId, width, height baseUrl, this.userId, width, height
); );
} }
return null; return null;

View File

@@ -52,13 +52,15 @@ utils.inherits(Room, EventEmitter);
* avatar URL wasn't explicitly set. Default: true. * avatar URL wasn't explicitly set. Default: true.
* @return {?string} the avatar URL or null. * @return {?string} the avatar URL or null.
*/ */
Room.prototype.getAvatarUrl = function(baseUrl, width, height, resizeMethod, allowDefault) { Room.prototype.getAvatarUrl = function(baseUrl, width, height, resizeMethod,
allowDefault) {
var roomAvatarEvent = this.currentState.getStateEvents("m.room.avatar", ""); var roomAvatarEvent = this.currentState.getStateEvents("m.room.avatar", "");
if (!roomAvatarEvent) { if (allowDefault === undefined) { allowDefault = true; }
if (!roomAvatarEvent && !allowDefault) {
return null; return null;
} }
if (allowDefault === undefined) { allowDefault = true; }
var mainUrl = roomAvatarEvent.getContent().url; var mainUrl = roomAvatarEvent ? roomAvatarEvent.getContent().url : null;
if (mainUrl) { if (mainUrl) {
return ContentRepo.getHttpUriForMxc( return ContentRepo.getHttpUriForMxc(
baseUrl, mainUrl, width, height, resizeMethod baseUrl, mainUrl, width, height, resizeMethod
@@ -66,7 +68,7 @@ Room.prototype.getAvatarUrl = function(baseUrl, width, height, resizeMethod, all
} }
else if (allowDefault) { else if (allowDefault) {
return ContentRepo.getIdenticonUri( return ContentRepo.getIdenticonUri(
baseUrl, member.userId, width, height baseUrl, this.roomId, width, height
); );
} }

View File

@@ -40,7 +40,8 @@ describe("ContentRepo", function() {
function() { function() {
var mxcUri = "mxc://server.name/resourceid#automade"; var mxcUri = "mxc://server.name/resourceid#automade";
expect(ContentRepo.getHttpUriForMxc(baseUrl, mxcUri, 32)).toEqual( expect(ContentRepo.getHttpUriForMxc(baseUrl, mxcUri, 32)).toEqual(
baseUrl + "/_matrix/media/v1/thumbnail/server.name/resourceid?width=32#automade" baseUrl + "/_matrix/media/v1/thumbnail/server.name/resourceid" +
"?width=32#automade"
); );
}); });

View File

@@ -15,6 +15,40 @@ describe("RoomMember", function() {
member = new RoomMember(roomId, userA); member = new RoomMember(roomId, userA);
}); });
describe("getAvatarUrl", function() {
var hsUrl = "https://my.home.server";
it("should return the URL from m.room.member preferentially", function() {
member.events.member = utils.mkEvent({
event: true,
type: "m.room.member",
skey: userA,
room: roomId,
user: userA,
content: {
membership: "join",
avatar_url: "mxc://flibble/wibble"
}
});
var url = member.getAvatarUrl(hsUrl);
// we don't care about how the mxc->http conversion is done, other
// than it contains the mxc body.
expect(url.indexOf("flibble/wibble")).not.toEqual(-1);
});
it("should return an identicon HTTP URL if allowDefault was set and there " +
"was no m.room.member event", function() {
var url = member.getAvatarUrl(hsUrl, 64, 64, "crop", true);
expect(url.indexOf("http")).toEqual(0); // don't care about form
});
it("should return nothing if there is no m.room.member and allowDefault=false",
function() {
var url = member.getAvatarUrl(hsUrl, 64, 64, "crop", false);
expect(url).toEqual(null);
});
});
describe("setPowerLevelEvent", function() { describe("setPowerLevelEvent", function() {
it("should set 'powerLevel' and 'powerLevelNorm'.", function() { it("should set 'powerLevel' and 'powerLevelNorm'.", function() {
var event = utils.mkEvent({ var event = utils.mkEvent({

View File

@@ -20,6 +20,43 @@ describe("Room", function() {
room.currentState = utils.mock(sdk.RoomState, "currentState"); room.currentState = utils.mock(sdk.RoomState, "currentState");
}); });
describe("getAvatarUrl", function() {
var hsUrl = "https://my.home.server";
it("should return the URL from m.room.avatar preferentially", function() {
room.currentState.getStateEvents.andCallFake(function(type, key) {
if (type === "m.room.avatar" && key === "") {
return utils.mkEvent({
event: true,
type: "m.room.avatar",
skey: "",
room: roomId,
user: userA,
content: {
url: "mxc://flibble/wibble"
}
});
}
});
var url = room.getAvatarUrl(hsUrl);
// we don't care about how the mxc->http conversion is done, other
// than it contains the mxc body.
expect(url.indexOf("flibble/wibble")).not.toEqual(-1);
});
it("should return an identicon HTTP URL if allowDefault was set and there " +
"was no m.room.avatar event", function() {
var url = room.getAvatarUrl(hsUrl, 64, 64, "crop", true);
expect(url.indexOf("http")).toEqual(0); // don't care about form
});
it("should return nothing if there is no m.room.avatar and allowDefault=false",
function() {
var url = room.getAvatarUrl(hsUrl, 64, 64, "crop", false);
expect(url).toEqual(null);
});
});
describe("getMember", function() { describe("getMember", function() {
beforeEach(function() { beforeEach(function() {
// clobber members property with test data // clobber members property with test data