Skip to content

Commit

Permalink
test: add simple tests for validation
Browse files Browse the repository at this point in the history
  • Loading branch information
louisewang1 committed Mar 29, 2022
1 parent c24f153 commit a3b3a9d
Show file tree
Hide file tree
Showing 5 changed files with 139 additions and 1 deletion.
111 changes: 111 additions & 0 deletions cypress/integration/item/share/publishItem.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
import {
buildShareButtonId,
ITEM_PUBLISH_SECTION_TITLE_SELECTOR,
ITEM_VALIDATION_BUTTON_SELECTOR,
SHARE_ITEM_VISIBILITY_SELECT_ID,
} from '../../../../src/config/selectors';
import { buildItemPath } from '../../../../src/config/paths';
import {
ITEM_LOGIN_ITEMS,
SAMPLE_ITEMS,
SAMPLE_PUBLIC_ITEMS,
} from '../../../fixtures/items';
import { SETTINGS } from '../../../../src/config/constants';
import { DEFAULT_TAGS } from '../../../fixtures/itemTags';

const ITEM_PUBLISH_SECTION_TITLE = 'Publication On Explorer';

const openShareItemTab = (id) => {
cy.get(`#${buildShareButtonId(id)}`).click();
};

// eslint-disable-next-line import/prefer-default-export
export const changeVisibility = (value) => {
cy.get(`#${SHARE_ITEM_VISIBILITY_SELECT_ID}`).click();
cy.get(`li[data-value="${value}"]`).click();
};

describe('Change visibililty to published', () => {
it('Default Private Item', () => {
cy.setUpApi({ ...SAMPLE_ITEMS, tags: DEFAULT_TAGS });
const item = SAMPLE_ITEMS.items[0];
cy.visit(buildItemPath(item.id));
openShareItemTab(item.id);

// publication section should not exist at this time
cy.get(`#${ITEM_PUBLISH_SECTION_TITLE_SELECTOR}`).should('not.exist');

changeVisibility(SETTINGS.ITEM_PUBLISHED.name);
cy.get(`#${ITEM_PUBLISH_SECTION_TITLE_SELECTOR}`).should(
'have.text',
ITEM_PUBLISH_SECTION_TITLE,
);
// visibilitySelect value should not be changed
const visiblitySelect = cy.get(
`#${SHARE_ITEM_VISIBILITY_SELECT_ID} + input`,
);
visiblitySelect.should('have.value', SETTINGS.ITEM_PRIVATE.name);
});

it('Public Item', () => {
cy.setUpApi({ ...SAMPLE_PUBLIC_ITEMS, tags: DEFAULT_TAGS });
const item = SAMPLE_PUBLIC_ITEMS.items[0];
cy.visit(buildItemPath(item.id));
openShareItemTab(item.id);

// publication section should not exist at this time
cy.get(`#${ITEM_PUBLISH_SECTION_TITLE_SELECTOR}`).should('not.exist');

changeVisibility(SETTINGS.ITEM_PUBLISHED.name);
cy.get(`#${ITEM_PUBLISH_SECTION_TITLE_SELECTOR}`).should(
'have.text',
ITEM_PUBLISH_SECTION_TITLE,
);
// visibilitySelect value should not be changed
const visiblitySelect = cy.get(
`#${SHARE_ITEM_VISIBILITY_SELECT_ID} + input`,
);
visiblitySelect.should('have.value', SETTINGS.ITEM_PUBLIC.name);
});

it('Pseudonymized Item', () => {
const item = ITEM_LOGIN_ITEMS.items[0];
cy.setUpApi({ items: [item], tags: DEFAULT_TAGS });
cy.visit(buildItemPath(item.id));
openShareItemTab(item.id);

// publication section should not exist at this time
cy.get(`#${ITEM_PUBLISH_SECTION_TITLE_SELECTOR}`).should('not.exist');

changeVisibility(SETTINGS.ITEM_PUBLISHED.name);
cy.get(`#${ITEM_PUBLISH_SECTION_TITLE_SELECTOR}`).should(
'have.text',
ITEM_PUBLISH_SECTION_TITLE,
);
// visibilitySelect value should not be changed
const visiblitySelect = cy.get(
`#${SHARE_ITEM_VISIBILITY_SELECT_ID} + input`,
);
visiblitySelect.should('have.value', SETTINGS.ITEM_LOGIN.name);
});
});

describe('Validate item', () => {
it('Default Private Item', () => {
cy.setUpApi({ ...SAMPLE_ITEMS, tags: DEFAULT_TAGS });
const item = SAMPLE_ITEMS.items[0];
cy.visit(buildItemPath(item.id));
openShareItemTab(item.id);
changeVisibility(SETTINGS.ITEM_PUBLISHED.name);

// click validate item button
cy.get(`#${ITEM_VALIDATION_BUTTON_SELECTOR}`).click();

cy.wait('@postItemValidation').then((data) => {
const {
request: { url },
} = data;
expect(url.split('/')).contains(item.id);
});
});
});
3 changes: 3 additions & 0 deletions cypress/support/commands.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ import {
mockGetItemValidationAndReviews,
mockGetItemValidationStatuses,
mockGetItemValidationReviewStatuses,
mockPostItemValidation,
} from './server';
import './commands/item';
import './commands/navigation';
Expand Down Expand Up @@ -275,6 +276,8 @@ Cypress.Commands.add(
mockGetItemValidationReviewStatuses(statuses);

mockGetItemValidationAndReviews(itemValidationAndReview);

mockPostItemValidation();
},
);

Expand Down
12 changes: 12 additions & 0 deletions cypress/support/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -1397,3 +1397,15 @@ export const mockGetItemValidationAndReviews = (itemValidationAndReviews) => {
},
).as('getItemValidationAndReviews');
};

export const mockPostItemValidation = () => {
cy.intercept(
{
method: DEFAULT_POST.method,
url: new RegExp(`${API_HOST}/items/validations/${ID_FORMAT}`),
},
({ reply, body }) => {
reply(body);
},
).as('postItemValidation');
};
11 changes: 10 additions & 1 deletion src/components/item/sharing/ItemPublishConfiguration.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ import {
SUBMIT_BUTTON_WIDTH,
} from '../../../config/constants';
import { CurrentUserContext } from '../../context/CurrentUserContext';
import {
ITEM_PUBLISH_SECTION_TITLE_SELECTOR,
ITEM_VALIDATION_BUTTON_SELECTOR,
} from '../../../config/selectors';

const { DELETE_ITEM_TAG, POST_ITEM_TAG, POST_ITEM_VALIDATION } = MUTATION_KEYS;
const {
Expand Down Expand Up @@ -220,7 +224,11 @@ const ItemPublishConfiguration = ({
return (
<>
<Divider className={classes.divider} />
<Typography variant="h6" className={classes.heading}>
<Typography
variant="h6"
className={classes.heading}
id={ITEM_PUBLISH_SECTION_TITLE_SELECTOR}
>
{t('Publication On Explorer')}
</Typography>
<Typography variant="body1">
Expand All @@ -240,6 +248,7 @@ const ItemPublishConfiguration = ({
{t('You need to validate your item before publish it.')}
</Typography>
<Button
id={ITEM_VALIDATION_BUTTON_SELECTOR}
variant="outlined"
onClick={handleValidate}
color="primary"
Expand Down
3 changes: 3 additions & 0 deletions src/config/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -176,3 +176,6 @@ export const buildCategoryMenuOptions = (menuName, optionIndex) =>
export const buildDashboardButtonId = (id) => `dashboard-button-${id}`;
export const buildGraaspAnalyzerId = (id) => `graasp-analyzer-${id}`;
export const buildPlayerTabName = (id) => `builder-tab-${id}`;

export const ITEM_PUBLISH_SECTION_TITLE_SELECTOR = 'itemPublishSectionTitle';
export const ITEM_VALIDATION_BUTTON_SELECTOR = 'itemValidationButton';

0 comments on commit a3b3a9d

Please sign in to comment.