Skip to content

Commit

Permalink
[Cloud Apps] Unit Testing (#4708)
Browse files Browse the repository at this point in the history
* add unit tests

* typo
  • Loading branch information
martinmckenna authored Mar 26, 2019
1 parent 25b3516 commit 5efcaf7
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 1 deletion.
27 changes: 27 additions & 0 deletions src/__data__/images.ts
Original file line number Diff line number Diff line change
Expand Up @@ -252,3 +252,30 @@ export const images: Linode.Image[] = [
description: null
}
];

export const privateImages: Linode.Image[] = [
{
created_by: 'linode',
deprecated: false,
id: 'linode/debian8.7',
vendor: 'Debian',
size: 1100,
type: 'manual',
created: '2017-08-15T22:28:13',
is_public: false,
label: 'Debian 8.7',
description: null
},
{
created_by: 'linode',
deprecated: false,
id: 'linode/containerlinux',
vendor: 'CoreOS',
size: 3000,
type: 'manual',
created: '2017-08-15T22:28:13',
is_public: false,
label: 'Container Linux',
description: null
}
];
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { images, privateImages } from 'src/__data__/images';
import { filterPublicImages, filterUDFErrors } from './formUtilities';

describe('Linode Create Utilities', () => {
it('should filter out public Images', () => {
const filteredImages = filterPublicImages([...images, ...privateImages]);
expect(
filteredImages.every(eachImage => !!eachImage.is_public)
).toBeTruthy();
});

it('should filter out all errors except UDF errors', () => {
const mockErrors: Linode.ApiFieldError[] = [
{
field: 'label',
reason: 'label is required'
},
{
field: 'ssh_keys',
reason: 'ssh_keys are required'
},
{
field: 'wp_password',
reason: 'a value for the UDF is required'
}
];

const errorResources = {
label: 'A label',
ssh_keys: 'ssh_keys'
};

const filteredErrors = filterUDFErrors(errorResources, mockErrors);
expect(filteredErrors[0].field).toBe('wp_password');
expect(filteredErrors).toHaveLength(1);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export const filterPublicImages = (images: Linode.Image[]) => {
};

/**
* filter out all the UDF errors from our error state.
* filter out all the API errors that aren't UDF errors from our error state.
* To do this, we compare the keys from the error state to our "errorResources"
* map and return all the errors that don't match the keys in that object
*
Expand Down

0 comments on commit 5efcaf7

Please sign in to comment.