Skip to content
This repository has been archived by the owner on Nov 27, 2022. It is now read-only.

Commit

Permalink
Delete activity when it exists.
Browse files Browse the repository at this point in the history
  • Loading branch information
RubenVerborgh committed Mar 20, 2019
1 parent 8e4e908 commit 70db965
Show file tree
Hide file tree
Showing 3 changed files with 125 additions and 25 deletions.
30 changes: 17 additions & 13 deletions src/components/ActivityButton.jsx
Expand Up @@ -19,29 +19,33 @@ export default function ActivityButton({
}) {
// Look up a possibly existing activity
object = srcToLDflex(object);
const [exists, setExists] = useState(false);
const [exists, setExists] = useState();
const activity = useLDflexValue(`${object}.findActivity("${activityType}")`);
if (activity && !exists)
if (exists === undefined && activity)
setExists(true);

// Creates a new activity (if none already exists)
async function createActivity() {
if (!exists) {
setExists(true);
try {
await data.resolve(`${object}.createActivity("${activityType}")`);
}
catch (error) {
setExists(false);
console.warn('@solid/react-components', error);
}
async function toggleActivity() {
// Optimistically display the result
setExists(!exists);
try {
// Try performing the action
const action = !exists ? 'create' : 'delete';
await data.resolve(`${object}.${action}Activity("${activityType}")`);
// Confirm the result (in case a concurrent action was pending)
setExists(!exists);
}
catch (error) {
// Revert to the previous state
setExists(exists);
console.warn('@solid/react-components', error);
}
}

// Return the activity button
className = `${className} ${exists ? 'performed' : ''}`;
return (
<button className={className} onClick={createActivity}>
<button className={className} onClick={toggleActivity}>
{ exists ? displayActivity : suggestActivity }
</button>
);
Expand Down
58 changes: 53 additions & 5 deletions test/components/ActivityButton-test.jsx
Expand Up @@ -20,6 +20,7 @@ describe('An ActivityButton', () => {
describe('without attributes', () => {
const findExpression = `[${currentUrl}].findActivity("${like}")`;
const createExpression = `[${currentUrl}].createActivity("${like}")`;
const deleteExpression = `[${currentUrl}].deleteActivity("${like}")`;

beforeEach(() => {
const { container } = render(<ActivityButton/>);
Expand Down Expand Up @@ -77,8 +78,15 @@ describe('An ActivityButton', () => {

describe('when activity creation succeeds', () => {
beforeEach(() => {
// mute `act` warning caused by asynchronous `reject`,
// since no workaround currently exists
// https://github.com/facebook/jest/issues/7151
console.mute();
activity.resolve({});
});
afterEach(() => {
console.unmute();
});

it('has "Like" as a label', () => {
expect(button).toHaveProperty('innerHTML', 'Like');
Expand All @@ -91,9 +99,6 @@ describe('An ActivityButton', () => {

describe('when activity creation fails', () => {
beforeEach(() => {
// mute `act` warning caused by asynchronous `reject`,
// since no workaround currently exists
// https://github.com/facebook/jest/issues/7151
console.mute();
activity.reject(new Error());
});
Expand Down Expand Up @@ -126,16 +131,59 @@ describe('An ActivityButton', () => {
});

describe('when clicked', () => {
let activity;
beforeEach(() => {
activity = new MockPromise();
data.resolve.mockReturnValue(activity);
fireEvent.click(button);
});

it('has "Like" as a label', () => {
expect(button).toHaveProperty('innerHTML', 'Like');
});

it('has the "performed" class', () => {
expect(button).toHaveClass('performed');
it('does not have the "performed" class', () => {
expect(button).not.toHaveClass('performed');
});

it('removes an activity', () => {
expect(data.resolve).toHaveBeenCalledWith(deleteExpression);
});

describe('when activity removal succeeds', () => {
beforeEach(() => {
console.mute();
activity.resolve({});
});
afterEach(() => {
console.unmute();
});

it('has "Like" as a label', () => {
expect(button).toHaveProperty('innerHTML', 'Like');
});

it('does not have the "performed" class', () => {
expect(button).not.toHaveClass('performed');
});
});

describe('when activity removal fails', () => {
beforeEach(() => {
console.mute();
activity.reject(new Error());
});
afterEach(() => {
console.unmute();
});

it('has "Like" as a label', () => {
expect(button).toHaveProperty('innerHTML', 'Like');
});

it('has the "performed" class', () => {
expect(button).toHaveClass('performed');
});
});
});
});
Expand Down
62 changes: 55 additions & 7 deletions test/components/LikeButton-test.jsx
Expand Up @@ -20,6 +20,7 @@ describe('A LikeButton', () => {
describe('without attributes', () => {
const findExpression = `[${currentUrl}].findActivity("${like}")`;
const createExpression = `[${currentUrl}].createActivity("${like}")`;
const deleteExpression = `[${currentUrl}].deleteActivity("${like}")`;

beforeEach(() => {
data.resolve.mockClear();
Expand Down Expand Up @@ -82,8 +83,15 @@ describe('A LikeButton', () => {

describe('when activity creation succeeds', () => {
beforeEach(() => {
// mute `act` warning caused by asynchronous `reject`,
// since no workaround currently exists
// https://github.com/facebook/jest/issues/7151
console.mute();
activity.resolve({});
});
afterEach(() => {
console.unmute();
});

it('has "You liked this page" as a label', () => {
expect(button).toHaveProperty('innerHTML', 'You liked this page');
Expand All @@ -96,9 +104,6 @@ describe('A LikeButton', () => {

describe('when activity creation fails', () => {
beforeEach(() => {
// mute `act` warning caused by asynchronous `reject`,
// since no workaround currently exists
// https://github.com/facebook/jest/issues/7151
console.mute();
activity.reject(new Error());
});
Expand Down Expand Up @@ -131,16 +136,59 @@ describe('A LikeButton', () => {
});

describe('when clicked', () => {
let activity;
beforeEach(() => {
activity = new MockPromise();
data.resolve.mockReturnValue(activity);
fireEvent.click(button);
});

it('has "You liked this page" as a label', () => {
expect(button).toHaveProperty('innerHTML', 'You liked this page');
it('has "Like this page" as a label', () => {
expect(button).toHaveProperty('innerHTML', 'Like this page');
});

it('has the "performed" class', () => {
expect(button).toHaveClass('performed');
it('does not have the "performed" class', () => {
expect(button).not.toHaveClass('performed');
});

it('creates an activity', () => {
expect(data.resolve).toHaveBeenCalledWith(deleteExpression);
});

describe('when activity creation succeeds', () => {
beforeEach(() => {
console.mute();
activity.resolve({});
});
afterEach(() => {
console.unmute();
});

it('has "Like this page" as a label', () => {
expect(button).toHaveProperty('innerHTML', 'Like this page');
});

it('does not have the "performed" class', () => {
expect(button).not.toHaveClass('performed');
});
});

describe('when activity creation fails', () => {
beforeEach(() => {
console.mute();
activity.reject(new Error());
});
afterEach(() => {
console.unmute();
});

it('has "You liked this page" as a label', () => {
expect(button).toHaveProperty('innerHTML', 'You liked this page');
});

it('has the "performed" class', () => {
expect(button).toHaveClass('performed');
});
});
});
});
Expand Down

0 comments on commit 70db965

Please sign in to comment.