mattermost-mobile/test/service/actions/files.test.js
Chris Duarte 3b7a6df617 PLT-5530 Render placeholder icons + metadata for file attachments in posts list (#249)
* Add icons for file types

* Compress image files for file type icons

* Add constants for file types

* Add utils for file metadata

* Add actions & reducers for fetching file attachments

* Render file attachments in posts

* Add reducers for handling files requests

* Refactor getFileType in file utils

* Refactor getFileIconPath in file utils

* Refactor getFormattedFileSize in file utils

* Trim trailing whitespace in getTruncatedFilename

* Style file attachment metadata

* Remove entity store reducer for files fetch failure

* Change filesForPost to fileIdsForPost in files store

* Use a selector for getFilesForPost

* Memoize getFilesForPost selector

* Dispatch postId with getFilesForPost

* Add test for getFilesForPost

* Upload seed file in getFilesForPost test

* Display correct icon for image attachments

* Fix reducers for receiving files for post

Expect response data to be array not object

* Style attachment post

* Merge headers (defaults + opts) in Client.doFetch

* Upload file as FormData

* Use form-data lib instead of built-in polyfill

* Associate file with post in getFilesForPost test

* Improve assertions in getFilesForPost test

* Check for createPost failure in getFilesForPost test

* Use correct post id in getFilesForPost test

* Use client not action to create post in getFilesForPost test

* Fix file upload in getFilesForPost test

* Add assertion for name of uploaded file

* Update attachment post style

* Fix spelling of loadFilesForPostIfNecessary

* Remove eslint-disable no-magic-numbers from file_utils.js

* Remove unused index prop from FileAttachment

* Clarify code for merging headers in Client.getOptions

* Dynamically truncate file attachment names

* Extract component for FileAttachmentIcon

* Use white bg for FileAttachmentIcon to match webapp styles

* Match webapp styles for file attachment borders

* Move file icon path lookup to FileAttachmentIcon

* Use more logical ordering for file type lookup
2017-02-17 17:40:40 -05:00

69 lines
2.4 KiB
JavaScript

// Copyright (c) 2017 Mattermost, Inc. All Rights Reserved.
// See License.txt for license information.
import fs from 'fs';
import assert from 'assert';
const FormData = require('form-data');
import * as Actions from 'service/actions/files';
import Client from 'service/client';
import configureStore from 'app/store';
import {RequestStatus} from 'service/constants';
import TestHelper from 'test/test_helper';
describe('Actions.Files', () => {
let store;
before(async () => {
await TestHelper.initBasic(Client);
});
beforeEach(() => {
store = configureStore();
});
after(async () => {
await TestHelper.basicClient.logout();
});
it('getFilesForPost', async () => {
const {basicClient, basicTeam, basicChannel} = TestHelper;
const testFileName = 'test.png';
const testImageData = fs.createReadStream(`test/assets/images/${testFileName}`);
const clientId = TestHelper.generateId();
const imageFormData = new FormData();
imageFormData.append('files', testImageData);
imageFormData.append('channel_id', basicChannel.id);
imageFormData.append('client_ids', clientId);
const formBoundary = imageFormData.getBoundary();
const fileUploadResp = await basicClient.
uploadFile(basicTeam.id, basicChannel.id, clientId, imageFormData, formBoundary);
const fileId = fileUploadResp.file_infos[0].id;
const fakePostForFile = TestHelper.fakePost(basicChannel.id);
fakePostForFile.file_ids = [fileId];
const postForFile = await basicClient.createPost(basicTeam.id, fakePostForFile);
await Actions.getFilesForPost(
basicTeam.id, basicChannel.id, postForFile.id
)(store.dispatch, store.getState);
const filesRequest = store.getState().requests.files.getFilesForPost;
const {files: allFiles, fileIdsByPostId} = store.getState().entities.files;
if (filesRequest.status === RequestStatus.FAILURE) {
throw new Error(JSON.stringify(filesRequest.error));
}
assert.ok(allFiles);
assert.ok(allFiles[fileId]);
assert.equal(allFiles[fileId].id, fileId);
assert.equal(allFiles[fileId].name, testFileName);
assert.ok(fileIdsByPostId);
assert.ok(fileIdsByPostId[postForFile.id]);
assert.equal(fileIdsByPostId[postForFile.id][0], fileId);
});
});