-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
adhocracy4/follows: on click redirect to login page if user is not lo…
…gged in
- Loading branch information
Showing
7 changed files
with
173 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
68 changes: 68 additions & 0 deletions
68
adhocracy4/follows/static/follows/__tests__/FollowButton.jest.jsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
import React from 'react' | ||
import { render, fireEvent, screen } from '@testing-library/react' | ||
import '@testing-library/jest-dom' | ||
import { FollowButton } from '../FollowButton' | ||
import api from '../../../../static/api' | ||
|
||
// mock api and config, as they rely on network and browser | ||
jest.mock('../../../../static/config') | ||
jest.mock('../../../../static/api') | ||
|
||
afterEach(() => { | ||
jest.clearAllMocks() | ||
}) | ||
|
||
test('Test render FollowButton not following', async () => { | ||
api.follow.setFollowing({ enabled: false }) | ||
render(<FollowButton authenticatedAs project="test" />) | ||
const followButton = await screen.findByText('Follow') | ||
expect(followButton).toBeTruthy() | ||
const followingButton = screen.queryByText('Following') | ||
expect(followingButton).toBeNull() | ||
expect(api.follow.get).toHaveBeenCalledTimes(1) | ||
}) | ||
|
||
test('Test render FollowButton following', async () => { | ||
api.follow.setFollowing({ enabled: true }) | ||
render(<FollowButton authenticatedAs project="test" />) | ||
const followingButton = await screen.findByText('Following') | ||
expect(followingButton).toBeTruthy() | ||
const followButton = screen.queryByText('Follow') | ||
expect(followButton).toBeNull() | ||
expect(api.follow.get).toHaveBeenCalledTimes(1) | ||
}) | ||
|
||
test('Test render FollowButton click follow', async () => { | ||
api.follow.setFollowing({ enabled: false }) | ||
render(<FollowButton authenticatedAs project="test" />) | ||
let followButton = await screen.findByText('Follow') | ||
expect(followButton).toBeTruthy() | ||
let followingButton = screen.queryByText('Following') | ||
expect(followingButton).toBeNull() | ||
fireEvent.click(followButton) | ||
followingButton = await screen.findByText('Following') | ||
expect(followingButton).toBeTruthy() | ||
followButton = screen.queryByText('Follow') | ||
expect(followButton).toBeNull() | ||
expect(api.follow.change).toHaveBeenCalledTimes(1) | ||
expect(api.follow.get).toHaveBeenCalledTimes(1) | ||
}) | ||
|
||
test('Test FollowButton redirect', async () => { | ||
// testing the redirect doesn't work and will throw an exception | ||
// as we are not in a browser. | ||
// workaround: delete location and simply check if href is set | ||
// to "correct" url | ||
delete window.location | ||
window.location = {} | ||
api.follow.setFollowing({ enabled: false }) | ||
render(<FollowButton authenticatedAs={null} project="test" />) | ||
const followButton = await screen.findByText('Follow') | ||
expect(followButton).toBeTruthy() | ||
const followingButton = screen.queryByText('Following') | ||
expect(followingButton).toBeNull() | ||
fireEvent.click(followButton) | ||
expect(window.location.href).toBe('/mock-url') | ||
expect(api.follow.change).not.toHaveBeenCalled() | ||
expect(api.follow.get).not.toHaveBeenCalled() | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,20 @@ | ||
import json | ||
|
||
from django import template | ||
from django.utils.html import format_html | ||
|
||
register = template.Library() | ||
|
||
|
||
@register.simple_tag() | ||
def react_follows(project): | ||
@register.simple_tag(takes_context=True) | ||
def react_follows(context, project): | ||
request = context["request"] | ||
user = request.user | ||
authenticated_as = None | ||
if user.is_authenticated: | ||
authenticated_as = user.username | ||
attributes = {"project": project.name, "authenticatedAs": authenticated_as} | ||
return format_html( | ||
'<span data-a4-widget="follows" data-project={project}></span>', | ||
project=project.slug, | ||
'<span data-a4-widget="follows" data-attributes="{attributes}"></span>', | ||
attributes=json.dumps(attributes), | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
let following = null | ||
|
||
const api = { | ||
follow: { | ||
get: jest.fn(() => { | ||
const instance = { | ||
done: (fn) => { | ||
if (following !== null) { | ||
fn(following) | ||
} | ||
return instance | ||
}, | ||
fail: (fn) => { | ||
if (following === null) { | ||
fn({ status: 400 }) | ||
} | ||
return instance | ||
} | ||
} | ||
return instance | ||
}), | ||
change: jest.fn((enabled) => { | ||
following = { enabled } | ||
const instance = { | ||
done: (fn) => { | ||
if (following !== null) { | ||
fn(following) | ||
} | ||
return instance | ||
}, | ||
fail: (fn) => { | ||
if (following === null) { | ||
fn({ status: 400 }) | ||
} | ||
return instance | ||
} | ||
} | ||
return instance | ||
}), | ||
setFollowing: (value) => { | ||
following = value | ||
} | ||
} | ||
} | ||
|
||
module.exports = api |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
module.exports = { | ||
getLoginUrl: () => '/mock-url' | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
### Changed | ||
|
||
- refactor follow to be functional and add aria described by for when no alert shown and use a4 prefix for classes so external style liberies can be used (story !7618/7701) | ||
- redirect to login page when follow button is pressed and user is not logged | ||
in |