...
 
Commits (2)
......@@ -16,12 +16,15 @@ import { activitiesServiceFaker } from '../../../__mocks__/fake/ActivitiesFaker'
import CommentList from '../../../src/comments/CommentList';
jest.mock('../../../src/newsfeed/NewsfeedService');
import { getSingle } from '../../../src/newsfeed/NewsfeedService';
import entitiesService from '../../../src/common/services/entities.service';
jest.mock('../../../src/newsfeed/activity/Activity', () => 'Activity');
jest.mock('../../../src/comments/CommentList', () => 'CommentList');
jest.mock('../../../src/common/components/CenteredLoading', () => 'CenteredLoading');
jest.mock('../../../src/comments/CommentsStore');
jest.mock('../../../src/comments/CommentsStoreProvider');
jest.mock('../../../src/common/services/entities.service');
describe('Activity screen component', () => {
......@@ -50,39 +53,27 @@ describe('Activity screen component', () => {
params: {entity: activitiesServiceFaker().load(1).activities[0]}
}
};
screen = shallow(
<ActivityScreen navigation={navigation}/>
);
expect(screen).toMatchSnapshot();
// should have a comment list component
expect(screen.find(CommentList)).toHaveLength(1);
});
it('should show loader until it loads the activity', async (done) => {
navigation = {
push: jest.fn(),
state: {
routeName: 'some',
params: {guid: '1'}
}
};
getSingle.mockResolvedValue(activitiesServiceFaker().load(1).activities[0]);
entitiesService.single.mockResolvedValue(navigation.state.params.entity);
screen = shallow(
<ActivityScreen navigation={navigation}/>
);
// shoul show loading
expect(screen).toMatchSnapshot();
screen.update();
// unmount
await screen.instance().componentDidMount();
// workaround to run after the async didmount
setImmediate(() => {
// should show the activity
expect(screen).toMatchSnapshot();
done();
});
jest.runAllTicks();
// await is important here!
await screen.update();
// should show the activity
expect(screen).toMatchSnapshot();
// should have a comment list component
expect(screen.find(CommentList)).toHaveLength(1);
});
});
// Jest Snapshot v1, https://goo.gl/fbAQLP
exports[`Activity screen component renders correctly with an entity as param 1`] = `
exports[`Activity screen component renders correctly with an entity as param 1`] = `<CenteredLoading />`;
exports[`Activity screen component renders correctly with an entity as param 2`] = `
<View
style={
Array [
......@@ -15,11 +17,9 @@ exports[`Activity screen component renders correctly with an entity as param 1`]
>
<CommentList
entity={
ActivityModel {
"__list": null,
Object {
"attachment_guid": false,
"blurb": false,
"comments:count": undefined,
"container_guid": "activityguid0",
"custom_data": false,
"custom_type": false,
......@@ -27,12 +27,9 @@ exports[`Activity screen component renders correctly with an entity as param 1`]
"edited": "",
"getThumbSource": [Function],
"guid": "activityguid0",
"is_visible": true,
"mature": false,
"mature_visibility": false,
"message": "Message",
"ownerObj": UserModel {
"__list": null,
"ownerObj": Object {
"getAvatarSource": [Function],
"guid": "824853017709780997",
"subtype": false,
......@@ -41,16 +38,10 @@ exports[`Activity screen component renders correctly with an entity as param 1`]
},
"owner_guid": "824853017709780997",
"parent_guid": "838106762591510528",
"paywall": undefined,
"perma_url": false,
"pinned": undefined,
"rowKey": "something0",
"shouldBeBlured": [MockFunction],
"thumbnail_src": false,
"thumbs:down:count": undefined,
"thumbs:down:user_guids": undefined,
"thumbs:up:count": undefined,
"thumbs:up:user_guids": undefined,
"title": "TITLE",
"type": "activity",
"wire_totals": Object {
......@@ -62,11 +53,9 @@ exports[`Activity screen component renders correctly with an entity as param 1`]
<Activity
autoHeight={false}
entity={
ActivityModel {
"__list": null,
Object {
"attachment_guid": false,
"blurb": false,
"comments:count": undefined,
"container_guid": "activityguid0",
"custom_data": false,
"custom_type": false,
......@@ -74,12 +63,9 @@ exports[`Activity screen component renders correctly with an entity as param 1`]
"edited": "",
"getThumbSource": [Function],
"guid": "activityguid0",
"is_visible": true,
"mature": false,
"mature_visibility": false,
"message": "Message",
"ownerObj": UserModel {
"__list": null,
"ownerObj": Object {
"getAvatarSource": [Function],
"guid": "824853017709780997",
"subtype": false,
......@@ -88,16 +74,10 @@ exports[`Activity screen component renders correctly with an entity as param 1`]
},
"owner_guid": "824853017709780997",
"parent_guid": "838106762591510528",
"paywall": undefined,
"perma_url": false,
"pinned": undefined,
"rowKey": "something0",
"shouldBeBlured": [MockFunction],
"thumbnail_src": false,
"thumbs:down:count": undefined,
"thumbs:down:user_guids": undefined,
"thumbs:up:count": undefined,
"thumbs:up:user_guids": undefined,
"title": "TITLE",
"type": "activity",
"wire_totals": Object {
......@@ -321,213 +301,3 @@ exports[`Activity screen component renders correctly with an entity as param 1`]
/>
</View>
`;
exports[`Activity screen component should show loader until it loads the activity 1`] = `<CenteredLoading />`;
exports[`Activity screen component should show loader until it loads the activity 2`] = `
<View
style={
Array [
Object {
"flex": 1,
},
Object {
"backgroundColor": "white",
},
]
}
>
<CommentList
entity={
ActivityModel {
"__list": null,
"comments:count": undefined,
"edited": undefined,
"is_visible": true,
"mature": undefined,
"mature_visibility": false,
"message": undefined,
"paywall": undefined,
"pinned": undefined,
"thumbs:down:count": undefined,
"thumbs:down:user_guids": undefined,
"thumbs:up:count": undefined,
"thumbs:up:user_guids": undefined,
}
}
header={
<Activity
autoHeight={false}
entity={
ActivityModel {
"__list": null,
"comments:count": undefined,
"edited": undefined,
"is_visible": true,
"mature": undefined,
"mature_visibility": false,
"message": undefined,
"paywall": undefined,
"pinned": undefined,
"thumbs:down:count": undefined,
"thumbs:down:user_guids": undefined,
"thumbs:up:count": undefined,
"thumbs:up:user_guids": undefined,
}
}
navigation={
Object {
"push": [MockFunction],
"state": Object {
"params": Object {
"guid": "1",
},
"routeName": "some",
},
}
}
/>
}
navigation={
Object {
"push": [MockFunction],
"state": Object {
"params": Object {
"guid": "1",
},
"routeName": "some",
},
}
}
onInputFocus={[Function]}
store={
Object {
"attachment": Object {},
"comments": Array [
Object {
"attachment_guid": false,
"blurb": false,
"container_guid": "guid0",
"custom_data": false,
"custom_type": false,
"description": "Congratulations! ",
"edited": "",
"guid": "guid0",
"mature": false,
"ownerObj": Object {
"guid": "824853017709780997",
"subtype": false,
"time_created": "1522036284",
"type": "user",
},
"owner_guid": "824853017709780997",
"parent_guid": "838106762591510528",
"perma_url": false,
"thumbnail_src": false,
"type": "comment",
},
Object {
"attachment_guid": false,
"blurb": false,
"container_guid": "guid1",
"custom_data": false,
"custom_type": false,
"description": "Congratulations! ",
"edited": "",
"guid": "guid1",
"mature": false,
"ownerObj": Object {
"guid": "824853017709780997",
"subtype": false,
"time_created": "1522036284",
"type": "user",
},
"owner_guid": "824853017709780997",
"parent_guid": "838106762591510528",
"perma_url": false,
"thumbnail_src": false,
"type": "comment",
},
Object {
"attachment_guid": false,
"blurb": false,
"container_guid": "guid2",
"custom_data": false,
"custom_type": false,
"description": "Congratulations! ",
"edited": "",
"guid": "guid2",
"mature": false,
"ownerObj": Object {
"guid": "824853017709780997",
"subtype": false,
"time_created": "1522036284",
"type": "user",
},
"owner_guid": "824853017709780997",
"parent_guid": "838106762591510528",
"perma_url": false,
"thumbnail_src": false,
"type": "comment",
},
Object {
"attachment_guid": false,
"blurb": false,
"container_guid": "guid3",
"custom_data": false,
"custom_type": false,
"description": "Congratulations! ",
"edited": "",
"guid": "guid3",
"mature": false,
"ownerObj": Object {
"guid": "824853017709780997",
"subtype": false,
"time_created": "1522036284",
"type": "user",
},
"owner_guid": "824853017709780997",
"parent_guid": "838106762591510528",
"perma_url": false,
"thumbnail_src": false,
"type": "comment",
},
Object {
"attachment_guid": false,
"blurb": false,
"container_guid": "guid4",
"custom_data": false,
"custom_type": false,
"description": "Congratulations! ",
"edited": "",
"guid": "guid4",
"mature": false,
"ownerObj": Object {
"guid": "824853017709780997",
"subtype": false,
"time_created": "1522036284",
"type": "user",
},
"owner_guid": "824853017709780997",
"parent_guid": "838106762591510528",
"perma_url": false,
"thumbnail_src": false,
"type": "comment",
},
],
"embed": RichEmbedStore {
"_richEmbedFetchTimer": null,
"clearRichEmbedAction": [Function],
"richEmbedUrl": "",
"setRichEmbedPromise": null,
},
"loadComments": [Function],
"loadNext": "aaaaaa",
"loadPrevious": "aaaaaa",
"post": [Function],
"setText": [Function],
}
}
/>
</View>
`;
export default {
getFromCache : jest.fn(),
garbageCollector : jest.fn(),
deleteFromCache : jest.fn(),
getFromFeed : jest.fn(),
single : jest.fn(),
fetch : jest.fn(),
addEntity : jest.fn(),
mapToModel : jest.fn(),
}
\ No newline at end of file
......@@ -154,8 +154,6 @@ class EntitiesService {
let entity = this.getFromCache(urn);
if (!entity) {
// from sql storage
const stored = await entitiesStorage.read(urn);
......
......@@ -18,7 +18,7 @@ export class EntitiesStorage {
*/
async save(entity) {
if (!entity.urn){
if (!entity.urn) {
return;
}
......
......@@ -3,21 +3,40 @@ import {
action
} from 'mobx';
import entitiesService from '../services/entities.service';
/**
* Single Entity Store
*/
class SingleEntityStore {
@observable entity;
@observable errorLoading = false;
@action
setEntity(entity) {
this.entity = entity;
}
@action
setErrorLoading(value) {
this.errorLoading = value;
}
async loadEntity(urn, defaultEntity) {
this.setErrorLoading(false);
try {
const entity = await entitiesService.single(urn, defaultEntity);
this.setEntity(entity);
} catch (err) {
this.setErrorLoading(true);
}
}
@action
clear() {
this.entity = null;
this.errorLoading = false;
}
}
......
......@@ -34,10 +34,6 @@ export default class ActivityScreen extends Component {
entityStore = new SingleEntityStore();
state = {
error: null
};
/**
* Constructor
* @param {object} props
......@@ -50,37 +46,18 @@ export default class ActivityScreen extends Component {
this.comments = commentsStoreProvider.get();
if (params.entity && (params.entity.guid || params.entity.entity_guid)) {
this.entityStore.setEntity(ActivityModel.checkOrCreate(params.entity));
const entity = this.entityStore.entity;
const urn = 'urn:entity:' + (params.entity.guid || params.entity.entity_guid);
if (entity._list && entity._list.metadataServie) {
entity._list.metadataServie.pushSource('single');
}
}
}
this.entityStore.loadEntity(urn, ActivityModel.checkOrCreate(params.entity));
/**
* Component did mount
*/
async componentDidMount() {
const params = this.props.navigation.state.params;
if (!this.entityStore.entity || params.hydrate) {
try {
const resp = await getSingle(params.guid || params.entity.guid || params.entity.entity_guid);
// if it has a list asigned we set it to the new entity
if (this.entityStore.entity) {
this.entityStore.entity.update(resp.activity);
} else {
this.entityStore.setEntity(ActivityModel.checkOrCreate(resp.activity));
}
} catch (e) {
this.setState({error: true});
logService.exception('[ActivityScreen]',e);
//console.error('Cannot hydrate activity', e);
// change metadata source
if (params.entity._list && params.entity._list.metadataServie) {
params.entity._list.metadataServie.pushSource('single');
}
} else {
const urn = 'urn:entity:' + params.guid;
this.entityStore.loadEntity(urn);
}
}
......@@ -119,11 +96,11 @@ export default class ActivityScreen extends Component {
* Render
*/
render() {
if (!this.entityStore.entity && !this.state.error) return <CenteredLoading />;
if (!this.entityStore.entity && !this.entityStore.errorLoading) return <CenteredLoading />;
return (
<View style={[CS.flexContainer, CS.backgroundWhite]}>
{
!this.state.error ?
!this.entityStore.errorLoading ?
<CommentList
header={this.getHeader()}
entity={this.entityStore.entity}
......