Skip to content

Commit

Permalink
test(cli-core): added tests for utils from cli-core package (#13080)
Browse files Browse the repository at this point in the history
* test: add tests to isResourceNameUnique util

* test: add tests for recursiveOmit util

* docs: fix prettier command

* docs: add git-secrets instruction

* docs: fix git-secrets link

* test: change test object name

* chore: dependency comments for utils
  • Loading branch information
mzarnawski authored Aug 8, 2023
1 parent 2941584 commit 1c83134
Show file tree
Hide file tree
Showing 5 changed files with 121 additions and 2 deletions.
3 changes: 2 additions & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -121,9 +121,10 @@ If the change is a breaking change ([as defined by semantic versioning](https://
1. Within your local fork, create a new branch based on the issue you're addressing - e.g. `git checkout -b category-auth/admin-auth-support`
- Use grouping tokens at the beginning of the branch names. For e.g, if you are working on changes specific to `amplify-category-auth`, then you could start the branch name as `category-auth/...`
- Use slashes to separate parts of branch names
1. Before your first commit, install [git-secrets plugin](https://github.com/awslabs/git-secrets#installing-git-secrets)
1. Once your work is committed and you're ready to share, run `yarn test`. Manually test your changes in a sample app with different edge cases and also test across different platforms if possible.
1. Run `yarn lint-fix` to find and fix any linting errors
1. Run `yarn prettify:changes` to fix styling issues
1. Run `yarn prettier-changes` to fix styling issues
1. Then, push your branch: `git push origin HEAD` (pushes the current branch to origin remote)
1. Open GitHub to create a PR from your newly published branch. Fill out the PR template and submit a PR.
1. Finally, the Amplify CLI team will review your PR. Add reviewers based on the core member who is tracking the issue with you or code owners. _In the meantime, address any automated check that fail (such as linting, unit tests, etc. in CI)_
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,23 @@ stateManager_mock.getMeta.mockReturnValue({
},
});

test('conflict exists if names differ by case only', () => {
test('conflict exists if names differ by case only - throw error', () => {
expect(() => isResourceNameUnique('api', 'testblog')).toThrowErrorMatchingInlineSnapshot(
`"A resource named 'testBlog' already exists. Amplify resource names must be unique and are case-insensitive."`,
);
});

test('conflict exists - exit without throwing error', () => {
const result = isResourceNameUnique('api', 'testBlog', false);
expect(result).toBe(false);
});

test('conflict does not exist if names differ by characters', () => {
const result = isResourceNameUnique('api', 'newname');
expect(result).toBe(true);
});

test('conflict does not exist if category not found', () => {
const result = isResourceNameUnique('nosuchcategory', 'newname');
expect(result).toBe(true);
});
104 changes: 104 additions & 0 deletions packages/amplify-cli-core/src/__tests__/utils/recursiveOmit.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
import { recursiveOmit } from '../../utils';
import { $TSObject } from '../..';

describe('recursiveOmit', () => {
let testObject: $TSObject;

beforeEach(() => {
testObject = {
prop1: {
prop2: {
prop3: 'val3',
prop4: 'val4',
},
prop5: {
prop6: 'val6',
},
},
};
});

test('empty path does not mutate object', () => {
const result = {
prop1: {
prop2: {
prop3: 'val3',
prop4: 'val4',
},
prop5: {
prop6: 'val6',
},
},
};

recursiveOmit(testObject, []);
expect(testObject).toEqual(result);
});

test('wrong path does not mutate object', () => {
const result = {
prop1: {
prop2: {
prop3: 'val3',
prop4: 'val4',
},
prop5: {
prop6: 'val6',
},
},
};

recursiveOmit(testObject, ['prop1', 'prop7']);
expect(testObject).toEqual(result);
});

test('deleting a key with subkeys removes them all', () => {
const result = {};

recursiveOmit(testObject, ['prop1']);
expect(testObject).toEqual(result);
});

test('deleting a key with subkeys does not mutate sibling keys and subkeys', () => {
const result = {
prop1: {
prop5: {
prop6: 'val6',
},
},
};

recursiveOmit(testObject, ['prop1', 'prop2']);
expect(testObject).toEqual(result);
});

test('deleting a key in a specific path does not affect sibling keys if there are any', () => {
const result = {
prop1: {
prop2: {
prop4: 'val4',
},
prop5: {
prop6: 'val6',
},
},
};

recursiveOmit(testObject, ['prop1', 'prop2', 'prop3']);
expect(testObject).toEqual(result);
});

test('deleting a key in a specific path results in deleting keys that no longer have child keys', () => {
const result = {
prop1: {
prop2: {
prop3: 'val3',
prop4: 'val4',
},
},
};

recursiveOmit(testObject, ['prop1', 'prop5', 'prop6']);
expect(testObject).toEqual(result);
});
});
3 changes: 3 additions & 0 deletions packages/amplify-cli-core/src/utils/doc-links.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export function getGraphQLTransformerFunctionDocLink(version: number): string {
}
}

// Currently not used in this project, but there are dependencies in other projects https://github.com/search?q=org%3Aaws-amplify+getGraphQLTransformerAuthDocLink&type=code
export function getGraphQLTransformerAuthDocLink(version: number): string {
switch (version) {
case 1:
Expand All @@ -20,6 +21,7 @@ export function getGraphQLTransformerAuthDocLink(version: number): string {
}
}

// Currently not used in this project, but there are dependencies in other projects https://github.com/search?q=org%3Aaws-amplify+getGraphQLTransformerAuthSubscriptionsDocLink&type=code
export function getGraphQLTransformerAuthSubscriptionsDocLink(version: number): string {
switch (version) {
case 1:
Expand All @@ -42,6 +44,7 @@ export function getGraphQLTransformerOpenSearchDocLink(version: number): string
}
}

// Currently not used in this project, but there are dependencies in other projects https://github.com/search?q=org%3Aaws-amplify+getGraphQLTransformerOpenSearchProductionDocLink&type=code
export function getGraphQLTransformerOpenSearchProductionDocLink(version: number): string {
switch (version) {
case 1:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { stateManager } from '../state-manager';

// Currently not used in this project, but there are dependencies in other projects https://github.com/search?q=org%3Aaws-amplify+isResourceNameUnique&type=code
export const isResourceNameUnique = (category: string, resourceName: string, throwOnMatch = true) => {
const meta = stateManager.getMeta();
const resourceNames = Object.keys(meta?.[category] || {});
Expand Down

0 comments on commit 1c83134

Please sign in to comment.