Skip to content

Commit

Permalink
Add tracking and testing
Browse files Browse the repository at this point in the history
  • Loading branch information
aswasif007 committed Oct 12, 2023
1 parent 9b64136 commit 8314de3
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 4 deletions.
58 changes: 58 additions & 0 deletions __tests__/commands/phpmyadmin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/* eslint-disable @typescript-eslint/no-explicit-any,@typescript-eslint/no-unsafe-assignment,@typescript-eslint/no-unsafe-return */
/**
* External dependencies
*/

/**
* Internal dependencies
*/
import { CommandTracker } from '../../src/lib/tracker';
import { PhpMyAdminCommand } from '../../src/commands/phpmyadmin';
import API from '../../src/lib/api';
import { beforeEach, describe, expect, it, jest } from '@jest/globals';
import opn from 'opn';

const mutationMock = jest.fn( async () => {
return Promise.resolve( {
data: {
generatePHPMyAdminAccess: {
url: 'http://test-url.com',
},
},
} );
} );

jest.mock( '../../src/lib/api' );
jest.mock( 'opn' );
jest.mocked( API ).mockImplementation( () => {
return Promise.resolve( {
mutate: mutationMock,
} as any );
} );

describe( 'commands/PhpMyAdminCommand', () => {
beforeEach( () => {} );

describe( '.run', () => {
const app = { id: 123 };
const env = { id: 456, jobs: [] };
const tracker = jest.fn() as CommandTracker;
const cmd = new PhpMyAdminCommand( app, env, tracker );

it( 'should generate a URL by calling the right mutation', async () => {
await cmd.run();
expect( mutationMock ).toHaveBeenCalledWith( {
mutation: expect.anything(),
variables: {
input: {
environmentId: 456,
},
},
} );
} );
it( 'should open the generated URL in browser', async () => {
await cmd.run();
expect( opn ).toHaveBeenCalledWith( 'http://test-url.com', { wait: false } );
} );
} );
} );
22 changes: 18 additions & 4 deletions src/commands/phpmyadmin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import API, {
import * as exit from '../lib/cli/exit';
import { CommandTracker } from '../lib/tracker';
import { App, AppEnvironment } from '../graphqlTypes';
import { GraphQLFormattedError } from 'graphql';

export const GENERATE_PHP_MY_ADMIN_URL_MUTATION = gql`
mutation GeneratePhpMyAdminAccess($input: GeneratePhpMyAdminAccessInput) {
Expand Down Expand Up @@ -69,14 +70,27 @@ export class PhpMyAdminCommand {
this.silent = silent;

if ( ! this.env.id ) {
exit.withError( 'Please specify an environment' );
exit.withError( 'No environment was specified' );
}

this.log( 'Generating PhpMyAdmin URL...' );
const url = await generatePhpMyAdminAccess( this.env.id );

void opn( url, { wait: false } );
let url;
try {
url = await generatePhpMyAdminAccess( this.env.id );
} catch ( err ) {
const error = err as Error & {
graphQLErrors?: GraphQLFormattedError[];
};
void this.track( 'error', {
error_type: 'generate_pma_url',
error_message: error.message,
stack: error.stack,
} );
exit.withError( 'Failed to generate PhpMyAdmin URL' );
}

this.log( 'Switch to your default browser.' );
void opn( url, { wait: false } );
this.log( 'PhpMyAdmin is opened in your default browser.' );
}
}

0 comments on commit 8314de3

Please sign in to comment.