Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove API request consolidation across tabs. #3256

Merged
merged 8 commits into from
Aug 11, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/frontendtest.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,4 @@ jobs:
yarn --frozen-lockfile
npm rebuild node-sass
- name: Run tests
run: yarn run test-jest
run: yarn run test
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,10 @@
},
featureFlagValue() {
return function(key) {
return this.loading ? false : this.details.feature_flags[key] || false;
return this.loading
? false
: (this.details && this.details.feature_flags && this.details.feature_flags[key]) ||
false;
};
},
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,12 @@ const makeWrapper = ({ store, topicId = TOPIC.id }) => {
localVue,
router,
store,
stubs: {
NodePanel: true,
},
});
};

function getNodeListItems(wrapper) {
return wrapper.findAll('[data-test="node-list-item"]');
}

function hasEditSelectedBtn(wrapper) {
return wrapper.contains('[data-test="edit-selected-btn"]');
}
Expand All @@ -77,11 +76,8 @@ function hasDeleteSelectedBtn(wrapper) {
return wrapper.contains('[data-test="delete-selected-btn"]');
}

function selectNode(wrapper, nodeIdx) {
const nodeCheckbox = getNodeListItems(wrapper)
.at(nodeIdx)
.find('input[type="checkbox"]');
nodeCheckbox.setChecked();
function selectNode(wrapper) {
wrapper.vm.selected = [NODE_1.id];
}

describe('CurrentTopicView', () => {
Expand All @@ -99,9 +95,6 @@ describe('CurrentTopicView', () => {
global.CHANNEL_EDIT_GLOBAL.channel_id = CHANNEL.id;

const storeConfig = cloneDeep(STORE_CONFIG);
// `loadChildren` call needs to be resolved for NodePanel
// to finish loading (see NodePanel's `created` hook)
jest.spyOn(storeConfig.modules.contentNode.actions, 'loadChildren').mockResolvedValue();
store = storeFactory(storeConfig);

store.commit('channel/ADD_CHANNEL', CHANNEL);
Expand All @@ -117,14 +110,6 @@ describe('CurrentTopicView', () => {
jest.resetAllMocks();
});

it('should display all nodes of a topic', () => {
const nodeListItems = getNodeListItems(wrapper);

expect(nodeListItems.length).toBe(2);
expect(nodeListItems.at(0).text()).toContain('Test node 1');
expect(nodeListItems.at(1).text()).toContain('Test node 2');
});

it("shouldn't display any nodes operations buttons when no nodes are selected", () => {
expect(hasEditSelectedBtn(wrapper)).toBe(false);
expect(hasCopySelectedToClipboardBtn(wrapper)).toBe(false);
Expand All @@ -135,7 +120,7 @@ describe('CurrentTopicView', () => {

describe("when a user can't edit a channel", () => {
it('should display only copy to clipboard button when some nodes are selected', () => {
selectNode(wrapper, 0);
selectNode(wrapper);

expect(hasCopySelectedToClipboardBtn(wrapper)).toBe(true);
expect(hasEditSelectedBtn(wrapper)).toBe(false);
Expand All @@ -151,7 +136,7 @@ describe('CurrentTopicView', () => {
});

it('should display all nodes operations buttons when some nodes are selected', () => {
selectNode(wrapper, 0);
selectNode(wrapper);

expect(hasCopySelectedToClipboardBtn(wrapper)).toBe(true);
expect(hasEditSelectedBtn(wrapper)).toBe(true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@
currentLocationId() {
// If opening modal from inside TrashModal, begin navigation at root node
if (this.movingFromTrash) {
return this.currentChannel.root_id;
return this.currentChannel && this.currentChannel.root_id;
}
const contentNode = this.getContentNode(this.moveNodeIds[0]);
return contentNode && contentNode.parent;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ function makeWrapper(items) {
methods: {
loadContentNodes: jest.fn(),
loadAncestors: jest.fn(),
loadChildren: jest.fn(() => Promise.resolve()),
},
stubs: {
ResourceDrawer: true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@ describe('contentNode actions', () => {
return ContentNode.put(contentNodeDatum).then(newId => {
id = newId;
contentNodeDatum.id = newId;
jest
.spyOn(ContentNode, 'fetchCollection')
.mockImplementation(() => Promise.resolve([contentNodeDatum]));
jest
.spyOn(ContentNode, 'fetchModel')
.mockImplementation(() => Promise.resolve(contentNodeDatum));
return ContentNode.put({ title: 'notatest', parent: newId, lft: 2 }).then(() => {
store = storeFactory({
modules: {
Expand All @@ -34,6 +40,7 @@ describe('contentNode actions', () => {
});
});
afterEach(() => {
jest.restoreAllMocks();
return ContentNode.table.toCollection().delete();
});
describe('loadContentNodes action', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export function fetchResourceSearchResults(context, params) {
export function loadChannels(context, params) {
// Used for search channel filter dropdown
params.page_size = 25;
return Channel.requestCollection({ deleted: false, ...params }).then(channelPage => {
return Channel.fetchCollection({ deleted: false, ...params }).then(channelPage => {
return channelPage;
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,13 @@ const NEW_CHANNEL_SET = {
[NEW_OBJECT]: true,
};

const loadChannelSetMock = (cs, store) => {
return jest.fn().mockImplementation(() => {
store.commit('channelSet/ADD_CHANNELSET', cs);
return Promise.resolve(cs);
});
};

const makeWrapper = ({ store, channelSetId }) => {
if (router.currentRoute.name !== RouteNames.CHANNEL_SET_DETAILS) {
router.push({
Expand All @@ -55,21 +62,24 @@ const makeWrapper = ({ store, channelSetId }) => {
});
}

return mount(ChannelSetModal, {
const loadChannelSet = loadChannelSetMock(CHANNEL_SET, store);
const loadChannelList = jest.fn().mockImplementation(() => Promise.resolve(CHANNEL_SET.channels));

const wrapper = mount(ChannelSetModal, {
propsData: {
channelSetId,
},
methods: {
loadChannelSet,
loadChannelList,
},
router,
localVue,
store,
});
};

const loadChannelSetMock = channelSet => {
return jest.fn().mockImplementation(({ commit }) => {
commit('ADD_CHANNELSET', channelSet);
return Promise.resolve(channelSet);
});
wrapper.loadChannelSet = loadChannelSet;
wrapper.loadChannelList = loadChannelList;
return wrapper;
};

const getCollectionNameInput = wrapper => {
Expand Down Expand Up @@ -110,54 +120,47 @@ describe('ChannelSetModal', () => {
});

describe('if there are no data for a channel set yet', () => {
let loadChannelSet, loadChannelList;

let wrapper;
beforeEach(() => {
const storeConfig = cloneDeep(STORE_CONFIG);
loadChannelSet = loadChannelSetMock(CHANNEL_SET);
loadChannelList = jest.fn();
storeConfig.modules.channelSet.actions.loadChannelSet = loadChannelSet;
storeConfig.modules.channel.actions.loadChannelList = loadChannelList;

const store = storeFactory(storeConfig);

makeWrapper({ store, channelSetId: CHANNEL_SET.id });
wrapper = makeWrapper({ store, channelSetId: CHANNEL_SET.id });
});

it('should load the channel set', () => {
expect(loadChannelSet).toHaveBeenCalledTimes(1);
expect(loadChannelSet.mock.calls[0][1]).toBe(CHANNEL_SET.id);
expect(wrapper.loadChannelSet).toHaveBeenCalledTimes(1);
expect(wrapper.loadChannelSet.mock.calls[0][0]).toBe(CHANNEL_SET.id);
});

it('should load channels of the channel set', () => {
expect(loadChannelList).toHaveBeenCalledTimes(1);
expect(loadChannelList.mock.calls[0][1]).toEqual({ id__in: [CHANNEL_1.id, CHANNEL_2.id] });
expect(wrapper.loadChannelList).toHaveBeenCalledTimes(1);
expect(wrapper.loadChannelList.mock.calls[0][0]).toEqual({
id__in: [CHANNEL_1.id, CHANNEL_2.id],
});
});
});

describe('if a channel set has been already loaded', () => {
let store, loadChannelSet, loadChannelList;
let store, wrapper;

beforeEach(() => {
const storeConfig = cloneDeep(STORE_CONFIG);
loadChannelSet = jest.fn();
loadChannelList = jest.fn();
storeConfig.modules.channelSet.actions.loadChannelSet = loadChannelSet;
storeConfig.modules.channel.actions.loadChannelList = loadChannelList;

store = storeFactory(storeConfig);
store.commit('channelSet/ADD_CHANNELSET', CHANNEL_SET);

makeWrapper({ store, channelSetId: CHANNEL_SET.id });
wrapper = makeWrapper({ store, channelSetId: CHANNEL_SET.id });
});

it("shouldn't load the channel set", () => {
expect(loadChannelSet).not.toHaveBeenCalled();
expect(wrapper.loadChannelSet).not.toHaveBeenCalled();
});

it('should load channels from the channel set', () => {
expect(loadChannelList).toHaveBeenCalledTimes(1);
expect(loadChannelList.mock.calls[0][1]).toEqual({ id__in: [CHANNEL_1.id, CHANNEL_2.id] });
expect(wrapper.loadChannelList).toHaveBeenCalledTimes(1);
expect(wrapper.loadChannelList.mock.calls[0][0]).toEqual({
id__in: [CHANNEL_1.id, CHANNEL_2.id],
});
});
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export function searchCatalog(context, params) {
params.published = true;
let promise;
if (context.rootGetters.loggedIn) {
promise = Channel.requestCollection(params);
promise = Channel.fetchCollection(params);
} else {
promise = Channel.searchCatalog(params);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import * as resources from '../resources';

Object.values(resources).forEach(resource => {
if (resource.requestCollection) {
resource.requestCollection = () => Promise.resolve([]);
if (resource.fetchCollection) {
resource.fetchCollection = () => Promise.resolve([]);
}
if (resource.requestModel) {
resource.requestModel = () => Promise.resolve({});
if (resource.fetchModel) {
resource.fetchModel = () => Promise.resolve({});
}
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -625,7 +625,7 @@ describe('ContentNode methods', () => {
describe('getByNodeIdChannelId method', () => {
let node,
collection,
requestCollection,
fetchCollection,
table = {};

beforeEach(() => {
Expand All @@ -640,7 +640,7 @@ describe('ContentNode methods', () => {
};

collection = [Object.assign({}, node)];
requestCollection = mockMethod('requestCollection', () => Promise.resolve(collection));
fetchCollection = mockMethod('fetchCollection', () => Promise.resolve(collection));
mockProperty('table', table);
});

Expand All @@ -650,17 +650,17 @@ describe('ContentNode methods', () => {
node
);
expect(table.get).toHaveBeenCalledWith({ '[node_id+channel_id]': [node_id, channel_id] });
expect(requestCollection).not.toBeCalled();
expect(fetchCollection).not.toBeCalled();
});

it('should use call requestCollection when missing locally', async () => {
it('should use call fetchCollection when missing locally', async () => {
const { node_id, channel_id } = node;
node = null;
await expect(ContentNode.getByNodeIdChannelId(node_id, channel_id)).resolves.toMatchObject(
collection[0]
);
expect(table.get).toHaveBeenCalledWith({ '[node_id+channel_id]': [node_id, channel_id] });
expect(requestCollection).toHaveBeenCalledWith({
expect(fetchCollection).toHaveBeenCalledWith({
_node_id_channel_id_: [node_id, channel_id],
});
});
Expand All @@ -671,7 +671,7 @@ describe('ContentNode methods', () => {
collection = [];
await expect(ContentNode.getByNodeIdChannelId(node_id, channel_id)).resolves.toBeFalsy();
expect(table.get).toHaveBeenCalledWith({ '[node_id+channel_id]': [node_id, channel_id] });
expect(requestCollection).toHaveBeenCalledWith({
expect(fetchCollection).toHaveBeenCalledWith({
_node_id_channel_id_: [node_id, channel_id],
});
});
Expand Down
11 changes: 0 additions & 11 deletions contentcuration/contentcuration/frontend/shared/data/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,6 @@ export const TABLE_NAMES = {
CHANGE_LOCKS_TABLE,
};

export const MESSAGES = {
FETCH_COLLECTION: 'FETCH_COLLECTION',
FETCH_MODEL: 'FETCH_MODEL',
REQUEST_RESPONSE: 'REQUEST_RESPONSE',
};

export const STATUS = {
SUCCESS: 'SUCCESS',
FAILURE: 'FAILURE',
};

export const APP_ID = 'KolibriStudio';

// Transaction sources
Expand Down
Loading