Skip to content

Commit

Permalink
rename send to fetch
Browse files Browse the repository at this point in the history
  • Loading branch information
brojd committed Mar 6, 2024
1 parent 7dd38b5 commit 838c34b
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -80,21 +80,21 @@ describe('MobileAppConnection', () => {

test('it shows a loading message if it is currently fetching the QR code', async () => {
mockRootStore({
sendBackendConfirmationCode: jest.fn().mockResolvedValueOnce('dfd'),
fetchBackendConfirmationCode: jest.fn().mockResolvedValueOnce('dfd'),
});

const component = render(<MobileAppConnection userPk={USER_PK} />);
expect(component.container).toMatchSnapshot();

await waitFor(() => {
expect(UserHelper.sendBackendConfirmationCode).toHaveBeenCalledTimes(1);
expect(UserHelper.sendBackendConfirmationCode).toHaveBeenCalledWith(USER_PK, BACKEND);
expect(UserHelper.fetchBackendConfirmationCode).toHaveBeenCalledTimes(1);
expect(UserHelper.fetchBackendConfirmationCode).toHaveBeenCalledWith(USER_PK, BACKEND);
});
});

test('it shows an error message if there was an error fetching the QR code', async () => {
mockRootStore({
sendBackendConfirmationCode: jest.fn().mockRejectedValueOnce('dfd'),
fetchBackendConfirmationCode: jest.fn().mockRejectedValueOnce('dfd'),
});

const component = render(<MobileAppConnection userPk={USER_PK} />);
Expand All @@ -103,29 +103,29 @@ describe('MobileAppConnection', () => {
await waitFor(() => {
expect(component.container).toMatchSnapshot();

expect(UserHelper.sendBackendConfirmationCode).toHaveBeenCalledTimes(1);
expect(UserHelper.sendBackendConfirmationCode).toHaveBeenCalledWith(USER_PK, BACKEND);
expect(UserHelper.fetchBackendConfirmationCode).toHaveBeenCalledTimes(1);
expect(UserHelper.fetchBackendConfirmationCode).toHaveBeenCalledWith(USER_PK, BACKEND);
});
});

test("it shows a QR code if the app isn't already connected", async () => {
mockRootStore({
sendBackendConfirmationCode: jest.fn().mockResolvedValueOnce('dfd'),
fetchBackendConfirmationCode: jest.fn().mockResolvedValueOnce('dfd'),
});

const component = render(<MobileAppConnection userPk={USER_PK} />);
expect(component.container).toMatchSnapshot();

await waitFor(() => {
expect(UserHelper.sendBackendConfirmationCode).toHaveBeenCalledTimes(1);
expect(UserHelper.sendBackendConfirmationCode).toHaveBeenCalledWith(USER_PK, BACKEND);
expect(UserHelper.fetchBackendConfirmationCode).toHaveBeenCalledTimes(1);
expect(UserHelper.fetchBackendConfirmationCode).toHaveBeenCalledWith(USER_PK, BACKEND);
});
});

test('if we disconnect the app, it disconnects and fetches a new QR code', async () => {
mockRootStore(
{
sendBackendConfirmationCode: jest.fn().mockResolvedValueOnce('dfd'),
fetchBackendConfirmationCode: jest.fn().mockResolvedValueOnce('dfd'),
unlinkBackend: jest.fn().mockResolvedValueOnce('asdfadsfafds'),
},
true
Expand All @@ -142,8 +142,8 @@ describe('MobileAppConnection', () => {
expect(component.container).toMatchSnapshot();

await waitFor(() => {
expect(UserHelper.sendBackendConfirmationCode).toHaveBeenCalledTimes(1);
expect(UserHelper.sendBackendConfirmationCode).toHaveBeenCalledWith(USER_PK, BACKEND);
expect(UserHelper.fetchBackendConfirmationCode).toHaveBeenCalledTimes(1);
expect(UserHelper.fetchBackendConfirmationCode).toHaveBeenCalledWith(USER_PK, BACKEND);

expect(rootStore.userStore.unlinkBackend).toHaveBeenCalledTimes(1);
expect(rootStore.userStore.unlinkBackend).toHaveBeenCalledWith(USER_PK, BACKEND);
Expand All @@ -153,7 +153,7 @@ describe('MobileAppConnection', () => {
test('it shows a loading message if it is currently disconnecting', async () => {
mockRootStore(
{
sendBackendConfirmationCode: jest.fn().mockResolvedValueOnce('dfd'),
fetchBackendConfirmationCode: jest.fn().mockResolvedValueOnce('dfd'),
unlinkBackend: jest.fn().mockResolvedValueOnce(new Promise((resolve) => setTimeout(resolve, 500))),
},
true
Expand All @@ -173,8 +173,8 @@ describe('MobileAppConnection', () => {
expect(component.container).toMatchSnapshot();

await waitFor(() => {
expect(UserHelper.sendBackendConfirmationCode).toHaveBeenCalledTimes(1);
expect(UserHelper.sendBackendConfirmationCode).toHaveBeenCalledWith(USER_PK, BACKEND);
expect(UserHelper.fetchBackendConfirmationCode).toHaveBeenCalledTimes(1);
expect(UserHelper.fetchBackendConfirmationCode).toHaveBeenCalledWith(USER_PK, BACKEND);

expect(rootStore.userStore.unlinkBackend).toHaveBeenCalledTimes(1);
expect(rootStore.userStore.unlinkBackend).toHaveBeenCalledWith(USER_PK, BACKEND);
Expand All @@ -184,7 +184,7 @@ describe('MobileAppConnection', () => {
test('it shows an error message if there was an error disconnecting the mobile app', async () => {
mockRootStore(
{
sendBackendConfirmationCode: jest.fn().mockResolvedValueOnce('dfd'),
fetchBackendConfirmationCode: jest.fn().mockResolvedValueOnce('dfd'),
unlinkBackend: jest.fn().mockRejectedValueOnce('asdfadsfafds'),
},
true
Expand All @@ -203,7 +203,7 @@ describe('MobileAppConnection', () => {
expect(component.container).toMatchSnapshot();

await waitFor(() => {
expect(UserHelper.sendBackendConfirmationCode).toHaveBeenCalledTimes(0);
expect(UserHelper.fetchBackendConfirmationCode).toHaveBeenCalledTimes(0);

expect(rootStore.userStore.unlinkBackend).toHaveBeenCalledTimes(1);
expect(rootStore.userStore.unlinkBackend).toHaveBeenCalledWith(USER_PK, BACKEND);
Expand All @@ -213,7 +213,7 @@ describe('MobileAppConnection', () => {
test('it polls loadUser on first render if not connected', async () => {
mockRootStore(
{
sendBackendConfirmationCode: jest.fn().mockResolvedValueOnce('dfd'),
fetchBackendConfirmationCode: jest.fn().mockResolvedValueOnce('dfd'),
unlinkBackend: jest.fn().mockRejectedValueOnce('asdfadsfafds'),
},
false
Expand All @@ -232,7 +232,7 @@ describe('MobileAppConnection', () => {
test('it polls loadUser after disconnect', async () => {
mockRootStore(
{
sendBackendConfirmationCode: jest.fn().mockResolvedValueOnce('dff'),
fetchBackendConfirmationCode: jest.fn().mockResolvedValueOnce('dff'),
unlinkBackend: jest.fn().mockRejectedValueOnce('asdff'),
},
true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ export const MobileAppConnection = observer(({ userPk }: Props) => {

try {
// backend verification code that we receive is a JSON object that has been "stringified"
const qrCodeContent = await UserHelper.sendBackendConfirmationCode(userPk, BACKEND);
const qrCodeContent = await UserHelper.fetchBackendConfirmationCode(userPk, BACKEND);
setQRCodeValue(qrCodeContent);
} catch (e) {
setErrorFetchingQRCode('There was an error fetching your QR code. Please try again.');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export const MSTeamsInfo = observer(() => {
const [onCallisAdded, setOnCallisAdded] = useState(false);

useEffect(() => {
UserHelper.sendBackendConfirmationCode(userStore.currentUserPk, 'MSTEAMS').then(setVerificationCode);
UserHelper.fetchBackendConfirmationCode(userStore.currentUserPk, 'MSTEAMS').then(setVerificationCode);
msteamsChannelStore.updateItems().then(() => {
const connectedChannels = msteamsChannelStore.getSearchResult();
if (connectedChannels?.length) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export const TelegramInfo = observer((_props: TelegramInfoProps) => {
const telegramConfigured = organizationStore.currentOrganization?.env_status.telegram_configured;

useEffect(() => {
UserHelper.sendTelegramConfirmationCode(userStore.currentUserPk).then((res) => {
UserHelper.fetchTelegramConfirmationCode(userStore.currentUserPk).then((res) => {
setVerificationCode(res.telegram_code);
setBotLink(res.bot_link);
});
Expand Down
4 changes: 2 additions & 2 deletions grafana-plugin/src/models/user/user.helpers.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,12 @@ export class UserHelper {
};
}

static async sendTelegramConfirmationCode(userPk: ApiSchemas['User']['pk']) {
static async fetchTelegramConfirmationCode(userPk: ApiSchemas['User']['pk']) {
return (await onCallApi().GET('/users/{id}/get_telegram_verification_code/', { params: { path: { id: userPk } } }))
.data;
}

static async sendBackendConfirmationCode(userPk: ApiSchemas['User']['pk'], backend: string) {
static async fetchBackendConfirmationCode(userPk: ApiSchemas['User']['pk'], backend: string) {
return (
await onCallApi().GET('/users/{id}/get_backend_verification_code/', {
params: { path: { id: userPk }, query: { backend } },
Expand Down
4 changes: 2 additions & 2 deletions grafana-plugin/src/models/user/user.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@ afterEach(() => {
jest.resetAllMocks();
});

describe('UserStore.sendBackendConfirmationCode', () => {
describe('UserStore.fetchBackendConfirmationCode', () => {
const userPk = '5';
const backend = 'dfkjfdjkfdkjfdaaa';
const mockedQrCode = 'dfkjfdkjfdkjfdjk';

test('it makes the proper API call and returns the response', async () => {
makeRequest.mockResolvedValueOnce(mockedQrCode);

expect(await UserHelper.sendBackendConfirmationCode(userPk, backend)).toEqual(mockedQrCode);
expect(await UserHelper.fetchBackendConfirmationCode(userPk, backend)).toEqual(mockedQrCode);

expect(makeRequest).toHaveBeenCalledTimes(1);
expect(makeRequest).toHaveBeenCalledWith(`/users/${userPk}/get_backend_verification_code?backend=${backend}`, {
Expand Down

0 comments on commit 838c34b

Please sign in to comment.