Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: device name parsing backport from v2 #5

Open
wants to merge 1 commit into
base: v1
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 1 addition & 12 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,15 +53,4 @@ module.exports = {
}
};

const deviceNameRegExp = new RegExp(/\((.*?)\)/);

const extractDeviceName = (fullDeviceName) => {
const matches = fullDeviceName.match(deviceNameRegExp);

if (matches && matches.length === 2) {
return matches[1];
}

console.error('Could not parse device name', fullDeviceName);
return 'unknown';
}
const extractDeviceName = require('./lib/utils/extractDeviceName');
39 changes: 39 additions & 0 deletions lib/utils/extractDeviceName.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
const DEVICE_NAME_REGEXP_1 = new RegExp(/\((.*?)\)/);
const DEVICE_NAME_REGEXP_2 = /^[0-9A-Fa-f-]{36}\s*(\{.*\})$/;

/**
* @param {String} fullDeviceName
*/
function extractDeviceName(fullDeviceName) {
const result = extract1(fullDeviceName) || extract2(fullDeviceName);
if (result) {
return result;
}

console.error('Could not parse device name', fullDeviceName);
return 'unknown';
}

/**
* @param {String} fullDeviceName
*/
function extract1(fullDeviceName) {
const match = fullDeviceName.match(DEVICE_NAME_REGEXP_1);
return match && match[1];
}

/**
* @param {String} fullDeviceName
*/
function extract2(fullDeviceName) {
const match = fullDeviceName.match(DEVICE_NAME_REGEXP_2);
const rawJson = match && match[1];

try {
return JSON.parse(rawJson).type;
} catch {
return undefined;
}
}

module.exports = extractDeviceName;
27 changes: 27 additions & 0 deletions lib/utils/extractDeviceName.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
const extractDeviceName = require('./extractDeviceName');

const TEST_UUID = 'EB449DF0-39DC-457A-B2D7-25CE51BB1A2E';

describe('extractDeviceName', () => {
beforeEach(() => {
jest.spyOn(console, 'error');
});

test.each([
[`${TEST_UUID} (iPhone X)`, 'iPhone X'],
[`${TEST_UUID} {"type": "iPhone 11"}`, 'iPhone 11'],
])('should parse %j to: %s', (input, expected) => {
expect(extractDeviceName(input)).toBe(expected);
expect(console.error).not.toHaveBeenCalled();
});

test.each([
[''],
['booted ()'],
[`${TEST_UUID} {}`],
[`${TEST_UUID} {some: 42}}`],
])('should identify %j as unknown and log an error', (input) => {
expect(extractDeviceName(input)).toBe('unknown');
expect(console.error).toHaveBeenCalledWith(expect.any(String), input);
});
});
11 changes: 10 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"description": "Utility to integrate applitools screenshot testing in your detox tests",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
"test": "jest"
},
"repository": {
"type": "git",
Expand All @@ -28,5 +28,14 @@
},
"peerDependencies": {
"detox": "*"
},
"devDependencies": {
"jest": "^27.2.2"
},
"jest": {
"resetMocks": true,
"testMatch": [
"<rootDir>/lib/**/*.test.js"
]
}
}