Skip to content

Commit

Permalink
test(categoryOrderHints): add integration and unit tests
Browse files Browse the repository at this point in the history
add unit tests and integration tests for updating categoryOrderHint by category name and category slug
  • Loading branch information
philippspo committed Dec 9, 2015
1 parent 6479ee2 commit bcb9d13
Show file tree
Hide file tree
Showing 2 changed files with 165 additions and 75 deletions.
137 changes: 93 additions & 44 deletions src/spec/integration/categoryOrderHint.spec.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,19 @@ Config = require '../../config'
TestHelpers = require './testhelpers'
Promise = require 'bluebird'

defaultProduct = (productTypeId, categoryId) =>
name:
en: 'test product'
productType:
typeId: 'product-type'
id: productTypeId
slug:
en: TestHelpers.uniqueId 'slug-'
categories: [
typeId: 'category'
id: categoryId
]

createImporter = ->
im = new Import Config
im.allowRemovalOfVariants = true
Expand All @@ -22,6 +35,7 @@ newCategory = (name = 'Category name') ->
en: name
slug:
en: uniqueId 'c'
externalId: 'externalCategoryId'

describe 'Import', ->

Expand All @@ -34,7 +48,7 @@ describe 'Import', ->
debug 'create a category to work with'
@client.categories.save(newCategory())
.then (results) =>
@categoryId = results.body.id
@category = results.body
debug "Created #{results.length} categories"

@productType = TestHelpers.mockProductType()
Expand Down Expand Up @@ -66,25 +80,80 @@ describe 'Import', ->

it 'should add categoryOrderHints', (done) ->

@client.products.save(
name:
en: 'test product'
productType:
typeId: 'product-type'
id: @productType.id
slug:
en: TestHelpers.uniqueId 'slug-'
categories: [
typeId: 'category'
id: @categoryId
]
)
@client.products.save(defaultProduct(@productType.id, @category.id))
.then (result) =>
@product = result.body
csv =
"""
productType,id,version,slug,categoryOrderHints
#{@productType.id},#{@product.id},#{@product.version},#{@product.slug},#{@category.id}:0.5
"""
im = createImporter(
continueOnProblems: true
)
im.import(csv)
.then (result) =>
expect(result[0]).toBe '[row 2] Product updated.'
@client.products.byId(@product.id).fetch()
.then (result) =>
expect(result.body.masterData.staged.categoryOrderHints).toEqual {"#{@category.id}": '0.5'}
done()
.catch (err) -> done _.prettify(err)

it 'should add categoryOrderHints when using an external category id', (done) ->

@client.products.save(defaultProduct(@productType.id, @category.id))
.then (result) =>
@product = result.body
csv =
"""
productType,id,version,slug,categoryOrderHints
#{@productType.id},#{@product.id},#{@product.version},#{@product.slug},externalCategoryId:0.5
"""
im = createImporter(
continueOnProblems: true
)
im.import(csv)
.then (result) =>
expect(result[0]).toBe '[row 2] Product updated.'
@client.products.byId(@product.id).fetch()
.then (result) =>
expect(result.body.masterData.staged.categoryOrderHints).toEqual {"#{@category.id}": '0.5'}
done()
.catch (err) -> done _.prettify(err)

it 'should add categoryOrderHints when using an category name', (done) ->

@client.products.save(defaultProduct(@productType.id, @category.id))
.then (result) =>
@product = result.body
csv =
"""
productType,id,version,slug,categoryOrderHints
#{@productType.id},#{@product.id},#{@product.version},#{@product.slug},#{@category.name.en}:0.5
"""
im = createImporter(
continueOnProblems: true
)
debug csv
im.import(csv)
.then (result) =>
expect(result[0]).toBe '[row 2] Product updated.'
@client.products.byId(@product.id).fetch()
.then (result) =>
expect(result.body.masterData.staged.categoryOrderHints).toEqual {"#{@category.id}": '0.5'}
done()
.catch (err) -> done _.prettify(err)

it 'should add categoryOrderHints when using an category slug', (done) ->

@client.products.save(defaultProduct(@productType.id, @category.id))
.then (result) =>
@product = result.body
csv =
"""
productType,id,version,slug,categoryOrderHints
#{@productType.id},#{@product.id},#{@product.version},#{@product.slug},#{@categoryId}:0.5
#{@productType.id},#{@product.id},#{@product.version},#{@product.slug},#{@category.slug.en}:0.5
"""
im = createImporter(
continueOnProblems: true
Expand All @@ -94,26 +163,16 @@ describe 'Import', ->
expect(result[0]).toBe '[row 2] Product updated.'
@client.products.byId(@product.id).fetch()
.then (result) =>
expect(result.body.masterData.staged.categoryOrderHints).toEqual {"#{@categoryId}": '0.5'}
expect(result.body.masterData.staged.categoryOrderHints).toEqual {"#{@category.id}": '0.5'}
done()
.catch (err) -> done _.prettify(err)

it 'should remove categoryOrderHints', (done) ->

@client.products.save(
name:
en: 'test product'
productType:
typeId: 'product-type'
id: @productType.id
slug:
en: TestHelpers.uniqueId 'slug-'
categories: [
typeId: 'category'
id: @categoryId
]
categoryOrderHints:
"#{@categoryId}": '0.5'
_.extend {}, defaultProduct(@productType.id, @category.id),
categoryOrderHints:
"#{@category.id}": '0.5'
)
.then (result) =>
@product = result.body
Expand All @@ -137,26 +196,16 @@ describe 'Import', ->
it 'should change categoryOrderHints', (done) ->

@client.products.save(
name:
en: 'test product'
productType:
typeId: 'product-type'
id: @productType.id
slug:
en: TestHelpers.uniqueId 'slug-'
categories: [
typeId: 'category'
id: @categoryId
]
categoryOrderHints:
"#{@categoryId}": '0.5'
_.extend {}, defaultProduct(@productType.id, @category.id),
categoryOrderHints:
"#{@category.id}": '0.5'
)
.then (result) =>
@product = result.body
csv =
"""
productType,id,version,slug,categoryOrderHints
#{@productType.id},#{@product.id},#{@product.version},#{@product.slug},#{@categoryId}: 0.9
#{@productType.id},#{@product.id},#{@product.version},#{@product.slug},#{@category.id}: 0.9
"""
im = createImporter(
continueOnProblems: true
Expand All @@ -166,6 +215,6 @@ describe 'Import', ->
expect(result[0]).toBe '[row 2] Product updated.'
@client.products.byId(@product.id).fetch()
.then (result) =>
expect(result.body.masterData.staged.categoryOrderHints).toEqual {"#{@categoryId}": '0.9'}
expect(result.body.masterData.staged.categoryOrderHints).toEqual {"#{@category.id}": '0.9'}
done()
.catch (err) -> done _.prettify(err)
103 changes: 72 additions & 31 deletions src/spec/mapping.spec.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -593,49 +593,90 @@ describe 'Mapping', ->
#
# expect(data.product).toEqual expectedProduct

it 'should should map the categoryOrderHints', (done) ->
describe '#mapCategoryOrderHints', ->

beforeEach ->

@exampleCategory =
id: 'categoryId'
name:
en: 'myCoolCategory',
slug:
en: 'slug-123'
externalId: 'myExternalId'
# mock the categories
@map.categories.buildMaps([
id: 'categoryId'
name: 'myCoolCategory',
slug:
de: 'slug-123'
externalId: 'myExternalId'
@exampleCategory
])
productType =
@productType =
id: 'myType'
attributes: []

@expectedProduct =
productType:
typeId: 'product-type'
id: 'myType'
name:
en: 'myProduct'
slug:
en: 'myproduct'
categories: []
categoryOrderHints: {
categoryId: '0.9'
}
masterVariant: {
id: 1
sku: 'x'
prices: []
attributes: []
images: []
}
variants: []

it 'should should map the categoryOrderHints using a category id', (done) ->
csv =
"""
productType,name,variantId,sku,categoryOrderHints
foo,myProduct,1,x,categoryId:0.9
foo,myProduct,1,x,#{@exampleCategory.id}:0.9
"""
@validator.parse csv
.then (parsed) =>
@map.header = parsed.header
@validator.validateOffline parsed.data
data = @map.mapProduct @validator.rawProducts[0], productType

expectedProduct =
productType:
typeId: 'product-type'
id: 'myType'
name:
en: 'myProduct'
slug:
en: 'myproduct'
categories: []
categoryOrderHints: {
categoryId: '0.9'
}
masterVariant: {
id: 1
sku: 'x'
prices: []
attributes: []
images: []
}
variants: []
expect(data.product).toEqual expectedProduct
data = @map.mapProduct @validator.rawProducts[0], @productType

expect(data.product).toEqual @expectedProduct
done()
.catch done

it 'should should map the categoryOrderHints using a category name', (done) ->
csv =
"""
productType,name,variantId,sku,categoryOrderHints
foo,myProduct,1,x,#{@exampleCategory.name.en}:0.9
"""
@validator.parse csv
.then (parsed) =>
@map.header = parsed.header
@validator.validateOffline parsed.data
data = @map.mapProduct @validator.rawProducts[0], @productType

expect(data.product).toEqual @expectedProduct
done()
.catch done

it 'should should map the categoryOrderHints using a category slug', (done) ->
csv =
"""
productType,name,variantId,sku,categoryOrderHints
foo,myProduct,1,x,#{@exampleCategory.slug.en}:0.9
"""
@validator.parse csv
.then (parsed) =>
@map.header = parsed.header
@validator.validateOffline parsed.data
data = @map.mapProduct @validator.rawProducts[0], @productType

expect(data.product).toEqual @expectedProduct
done()
.catch done

0 comments on commit bcb9d13

Please sign in to comment.